Java高手详解使用JDBC的步骤

开发 后端
本文将由Java高手详解使用JDBC的步骤,JDBC是大家使用比较多的连接数据库的方式,希望大家看过此文后有更多了解。

JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC为工具/数据库开发人员提供了一个标准的API,据此可以构建更高级的工具和接口,使数据库开发人员能够用纯 Java API 编写数据库应用程序,

使用JDBC的步骤分为6步

使用JDBC的步骤1. load the driver

(1)Class.forName()|Class.forName().newlnstance()|new DriverName()

(2)实例化时自动向DriverManager注册,不需要显示调用DriverManager.registerDriver

使用JDBC的步骤2. Connect to the DataBase

DriverManager.getConnection()

使用JDBC的步骤3.Excute the SQL

(1)connection.CreateStatement() 

(2)Statement.excuteQuery()

(3)Statement.executeUpdate()

使用JDBC的步骤4. Retrieve the result data

循环取得结果 while(rs.next())

使用JDBC的步骤5. show the result data

将数据库中的各种类型转换为java中的类型(getXXX)方法

使用JDBC的步骤6. close

close the resultset / close the  statement /close the connection

实际例子 Java代码

  1. package DB;     
  2. import java.sql.*;     
  3. class  Jdbc     
  4. {     
  5.     public static void main(String[] args)throws Exception     
  6.     {          
  7.         //只有下面2句话就可以连接到数据库中     
  8.         Class.forName("com.mysql.jdbc.Driver");        
  9.         Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "1234");    //Class.forName("com.mysql.jdbc.Driver");        
  10.         //Connection conn=(Connection) getConnection("jdbc:mysql://localhost:3306/drp", "root", "root");     
  11.  //Class.forName("oracal.jdbc.driver.OracalDriver");     
  12.         //new oracal.jdbc.driver.OracalDriver();     
  13.         //Connection conn=DriverManager.getConnection"jdbc:oracal:thin:@localhost:1521:SXT"."scott","tiger"     
  14.              
  15.         //jdbc.driverClassName=com.mysql.jdbc.Driver;     
  16.         //jdbcjdbc.url=jdbc:mysql:localhost:3306 /test?useUnicode=true&characterEncoding=utf8;     
  17.     }     
  18. }    
  19. package DB;  
  20. import java.sql.*;  
  21. class  Jdbc  
  22. {  
  23.  public static void main(String[] args)throws Exception  
  24.  {  
  25.     
  26.   //只有下面2句话就可以连接到数据库中  
  27.   Class.forName("com.mysql.jdbc.Driver");     
  28.   Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "1234");   
  29. //Class.forName("com.mysql.jdbc.Driver");     
  30.   //Connection conn=(Connection) getConnection("jdbc:mysql://localhost:3306/drp", "root", "root");  
  31. //Class.forName("oracal.jdbc.driver.OracalDriver");  
  32.   //new oracal.jdbc.driver.OracalDriver();  
  33.   //Connection conn=DriverManager.getConnection"jdbc:oracal:thin:@localhost:1521:SXT"."scott","tiger"  
  34.   //jdbc.driverClassName=com.mysql.jdbc.Driver;  
  35.   //jdbcjdbc.url=jdbc:mysql:localhost:3306 /test?useUnicode=true&characterEncoding=utf8;  
  36.  }  

还有另外的一个用try catch 的方法

下面就实际去操作一下

首先把mysql驱动mysql-connector-java-3.1.10-bin.jar 或者其它版本的驱动copy到WebRoot 下面的WEB-INF下面的lib里面

Java代码 

  1. package db;     
  2. //一定要注意类名字要相同!!     
  3. import java.sql.*;     
  4.  class  Jdbc     
  5. {     
  6.     public static void main(String[] args)throws Exception     
  7.     {     
  8.  Class.forName("com.mysql.jdbc.Driver");        
  9.         Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "1234");      
  10.         System.out.print("ok");//如果连接成功显示ok     
  11.     }     
  12. }    
  13. package db;  
  14. //一定要注意类名字要相同!!  
  15. import java.sql.*;  
  16. class  Jdbc  
  17. {  
  18.  public static void main(String[] args)throws Exception  
  19.  {  
  20.     Class.forName("com.mysql.jdbc.Driver");     
  21.   Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "1234");   
  22.   System.out.print("ok");//如果连接成功显示ok  
  23.  }  
  24. }  
  25. 然后接着看下面的升级版   
  26. Java代码   
  27. package db;     
  28.     
  29. import java.sql.*;     
  30.     
  31. import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;     
  32.     
  33. class Jdbc2 {     
  34.     public static void main(String[] args) throws Exception {     
  35.     
  36.         //1.先new 一个driver 然后向managerDriver注册     
  37.         Class.forName("com.mysql.jdbc.Driver");     
  38.         //2.通过DriverManager.getConnection传递个方法,严格的说是jdbc的url     
  39.         Connection conn = DriverManager.getConnection(     
  40.                 "jdbc:mysql://localhost:3306/test", "root", "1234");     
  41.         //3.创建个statement对象,执行sql语句     
  42.         Statement stmt = conn.createStatement();     
  43.         ResultSet rs = stmt.executeQuery("select * from test.admin");     
  44.         //4.取得结果集 5.对其进行便利     
  45.         while (rs.next()) {     
  46.             System.out.println(rs.getString("username"));     
  47.             System.out.println(rs.getInt("id"));     
  48.         }     
  49.         //6.关闭(要遵循后执行的先闭,先执行的后闭的原则)     
  50.         rs.close();     
  51.         stmt.close();     
  52.         conn.close();       
  53.     }     
  54. }       
  55. /**    
  56.  * 此例子需要注意的是:    
  57.  * 1.驱动是否在lib文件夹下面。    
  58.  * 2.数据库里面的库名以及表是否存在    
  59.  * 3."jdbc:mysql://localhost:3306/test", "root", "1234");    
  60.  * 分别对应的是地址、端口、库名、数据库的管理员名字、管理员密码。    
  61.  * 4."select * from test.admin" sql语句建议一定写的时候用 库名.表名。    
  62.  */    
  63. /*   

以上是jdbc 一个简单的例子,了解连接jdbc 的步骤。  

这段代码的统一出现的问题是在执行System.out.println(rs.getString("name"));的时候会出现exception,这样的话后面的close就不再被执行,占用缓存,最后导致服务器死机 (河北电信视频点击系统)改进的代码请看TESTHdbc3.java  

  1.  */    
  2.     
  3. //Class.forName("com.mysql.jdbc.Driver");        
  4. //Connection conn=(Connection) getConnection("jdbc:mysql://localhost:3306/drp", "root", "root");     
  5.     
  6. //Class.forName("oracal.jdbc.driver.OracalDriver");     
  7. //new oracal.jdbc.driver.OracalDriver();     
  8. //Connection conn=DriverManager.getConnection"jdbc:oracal:thin:@localhost:1521:SXT"."scott","tiger"     
  9. //jdbc.driverClassName=com.mysql.jdbc.Driver;     
  10. //jdbcjdbc.url=jdbc:mysql:localhost:3306 /test?useUnicode=true&characterEncoding=utf8;     
  11. /*    

以上是jdbc 一个简单的例子,了解连接jdbc 的步骤。  
 
这段代码的统一出现的问题是在执行System.out.println(rs.getString("name"));的时候会出现exception,这样的话后面的close就不再被执行,占用缓存,最后导致服务器死机(河北电信视频点击系统)改进的代码请看TESTHdbc3.java   

  1. //Class.forName("com.mysql.jdbc.Driver");     
  2.  //Connection conn=(Connection) getConnection("jdbc:mysql://localhost:3306/drp", "root", "root");  
  3.    
  4.  
  5.  
  6.  //Class.forName("oracal.jdbc.driver.OracalDriver");  
  7.  //new oracal.jdbc.driver.OracalDriver();  
  8.  //Connection conn=DriverManager.getConnection"jdbc:oracal:thin:@localhost:1521:SXT"."scott","tiger"  
  9.    
  10.  //jdbc.driverClassName=com.mysql.jdbc.Driver;  
  11.  //jdbcjdbc.url=jdbc:mysql:localhost:3306 /test?useUnicode=true&characterEncoding=utf8


最后让我们看个使用JDBC的步骤成熟版

Java代码

  1. package db;     
  2. import java.sql.*;     
  3. class Jdbc3 {     
  4.     public static void main(String[] args) {     
  5.         try {     
  6.     
  7.             Class.forName("com.mysql.jdbc.Driver");     
  8.     
  9.             Connection conn = DriverManager.getConnection(     
  10.                     "jdbc:mysql://localhost:3306/test", "root", "1234");     
  11.     
  12.             Statement stmt = conn.createStatement();     
  13.             ResultSet rs = stmt.executeQuery("select * from test.admin");     
  14.     
  15.             while (rs.next()) {     
  16.                 System.out.println(rs.getString("username"));     
  17.                 System.out.println(rs.getInt("id"));     
  18.             }     
  19.         } catch (ClassNotFoundException e) {     
  20.             e.printStackTrace();     
  21.         } catch(SQLException e){     
  22.             e.printStackTrace();}     
  23.         }     
  24.     finally{     
  25.         rs.close();     
  26.         stmt.close();     
  27.         conn.close();     
  28.     }           
  29. }   

【编辑推荐】

  1. 使用JDBC的五个精华功能
  2. Tomcat5+MySQL JDBC连接池配置
  3. 在Weblogic中实现JDBC的功能
  4. 详解JDBC与Hibernate区别
  5. JDBC连接MySQL数据库关键四步
  6. 浅谈JDBC的概念理解与学习

【责任编辑:彭凡 TEL:(010)68476606】

责任编辑:彭凡 来源: javaeye
相关推荐

2009-09-24 15:53:00

Hibernate J

2009-07-08 17:17:16

JDBC调用存储过程

2009-07-09 16:01:27

2010-06-17 15:33:16

SQL Server

2009-07-15 18:10:22

Java高手

2009-07-15 13:41:00

JDBC实例

2009-07-20 15:56:08

JDBC连接数据库步骤

2009-09-23 12:48:54

Hibernate I

2016-09-18 16:58:09

JavaProperties

2009-07-08 18:20:21

JDBC驱动

2009-06-10 18:11:58

Java高手

2015-09-09 08:45:49

JavaThreadLocal

2009-07-09 17:47:40

使用JDBC

2009-06-08 17:56:00

SpringJDBC事务

2021-09-29 09:42:32

AndroidViewDragHel拖动上下滑卡片

2009-07-23 15:17:54

JDBC连接Acces

2009-12-14 10:54:26

2013-05-23 14:25:44

JDBC

2021-07-14 14:27:01

AndroidAOPhugo

2009-04-09 09:19:25

C#规则表达式.NET
点赞
收藏

51CTO技术栈公众号