<span style="font-size:18px;">package com.hechao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 驱动(面向接口编程)
* @author hechao
*
*/
public class Test01 {
public static void main(String[] args) {
//1.加载驱动程序(反射)
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/Company",
"root",//用户名
"123456");//密码
//3.创建语句
Statement stmt = con.createStatement();
//4.发出SQL语句得到结果集(游标)
ResultSet rs = stmt.executeQuery("select * from emp");
// ResultSet r = stmt.executeQuery(
// "select ename as 员工姓名, sal as 最高工资 from emp" +
// " where sal = (select max(sal) from emp);");
//5.循环遍历结果集
while(rs.next()){//先判断游标是否可以取到下一条记录
System.out.println("姓名:" + rs.getString("ename"));
System.out.println("工资:" + rs.getInt("sal"));
System.out.println("*************");
}
// while(r.next()){
// System.out.println("员工姓名:" + r.getString("员工姓名"));
// System.out.println("最高工资:" + r.getInt("最高工资"));
// System.out.println("*************");
// }//同一时间只能查一个结果集
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(con != null){
try {
//关闭外部连接
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
</span>