public static void main(String []args) {
Connection con = null;
Statement sta = null;
ResultSet rs = null;
try {
Driver d = new com.mysql.jdbc.Driver();
DriverManager.registerDriver(d);
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1","root","xucanjie");
sta = con.createStatement();
String sql = "select e.ename,e.sal,d.dname from emp e join dept d on e.deptno=d.deptno";
rs=sta.executeQuery(sql);
while(rs.next()) {
System.out.println("员工姓名:" + rs.getString("ename") + " 员工部门:" + rs.getString("dname") + " 员工工资:" + rs.getInt("sal"));
}
} catch (SQLException 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(con!=null){
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
Connection con = null;
PreparedStatement psta = null;
ResultSet rs = null;
try {
Driver d = new com.mysql.jdbc.Driver();
DriverManager.registerDriver(d);
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1","root","xucanjie");
String sql = "select e.ename,e.sal,d.dname from emp e join dept d on e.deptno=d.deptno where sal>?";
psta = con.prepareStatement(sql);
psta.setInt(1,2500);
rs=psta.executeQuery();
while(rs.next()) {
System.out.println("员工姓名:" + rs.getString("ename") + " 员工部门:" + rs.getString("dname") + " 员工工资:" + rs.getInt("sal"));
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
if(rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(psta!=null){
try {
psta.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}