public static void main(String[] args) {
Connection conn = null;
Statement stmt = null ;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
Driver driver = new com.mysql.jdbc.Driver();
DriverManager.registerDriver(driver);
String url = "jdbc:mysql:localhost:3366/huae";
String user = "root";
String password = "1234";
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
String sql = "Select e.ename,d.dname from emp e join dept d on e.deptno = d.deptno";
rs = stmt.executeQuery(sql);
while(rs.next()){
String ename = rs.getString("ename");
String dname = rs.getString("dname");
System.out.println(ename+" "+dname);
}
} catch (Exception e) {
// TODO: handle exception
}finally{
if(rs!= null){
try {
rs.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
if(stmt!=null){
try {
stmt.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
if(conn!=null){
try {
conn.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
}