Connection conn = null;
Statement sta = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","(密码)");
sta = conn.createStatement();
String sql = "select ename,sal from emp;";
rs = sta.executeQuery(sql);
while(rs.next()){
String ename = rs.getString("ename");
String sal = rs.getString("sal");
System.out.println("名称:"+ename+",工资:"+sal);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (sta != null) {
try {
sta.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}