JDBC开发流程

2021/12/17 23:21:13

本文主要是介绍JDBC开发流程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

  1. 加载并注册JDBC驱动
  2. 创建数据库连接
  3. 创建Statement对象
  4. 遍历查询结果
  5. 关闭连接,释放资源

 

 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     

 



这篇关于JDBC开发流程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程