- 加载并注册JDBC驱动
- 创建数据库连接
- 创建Statement对象
- 遍历查询结果
- 关闭连接,释放资源
1 public class StandardJDBCSanple {
2 public static void main(String[] args) {
3 //1.加载并注册JDBC驱动
4 Connection conn =null;
5 try{
6 Class .forName("com.mysql.cj.jdbc.Driver");
7 //2.创建数据库连接
8 conn = DriverManager.getConnection(
9 url:""
10 user:"root",password:"root"
11
12 //3.创建Statement对象
13 Statement stmt = conn.createStatement();
14 ResultSet rs = stmt.executeQuery(sql:"select *from" employee");
15 //4.遍历查询结果
16 while(rs.next()){
17 Integer eno=rs.getInt(columnlndex:1);//eno
18 String ename = rs.getString(columnLabel:"ename");
19 Float salary = rs.getFloat(columnLabel:"salary");
20 String dname = rs.getString(columnLabel:dname");
21 System.out.println(dname + "-" + "-" + ename + "-" +salary);
22
23 }
24 }catch(Exception e){
25 e.printStacKTrace();
26 }finally {
27 try {
28 if(conn ! = null && conn.isClosed() == false){
29 //5.关闭连接,释放资源
30 conn.close();
31 }
32 }catch(Exception ex){
33 ex.pritnStackTrace();
34