Java学习(十四):JDBC方式连接数据库举例

java定义了JDBC这一标准的接口和类,为程序员操作数据库提供了统一的方式。

下载对应数据库的jar包,添加到工程内。

JDBC的操作方式比较单一,由五个流程组成:

1.通过数据库厂商提供的JDBC类库向DriverManager注册数据库驱动

2.使用DriverManager提供的getConnection()方法连接到数据库

3.通过数据库的连接对象的createStatement方法建立SQL语句对象

4.执行SQL语句,并将结果集合返回到ResultSet中

5.使用while循环读取结果

6.关闭数据库资源

代码举例

  1 import java.sql.Connection;
  2 import java.sql.DriverManager;
  3 import java.sql.ResultSet;
  4 import java.sql.SQLException;
  5 import java.sql.Statement;
  6 
  7 public class TestJDBC
  8 {
  9     private final static String url = "jdbc:mysql://localhost:3306/test";
 10     
 11     private final static String user = "";
 12     
 13     private final static String password = "";
 14     
 15     public static void main(String[] args)
 16     {
 17         ResultSet rs = null;
 18         Statement stmt = null;
 19         
 20         // jdbc驱动加载
 21         driverLoader();
 22         // 获取数据库连接
 23         Connection conn = connectionGet();
 24         try
 25         {
 26             stmt = conn.createStatement();
 27             String sql = "select * from ljf";
 28             rs = stmt.executeQuery(sql);
 29             while (rs.next())
 30             {
 31                 System.out.print(rs.getInt("id") + "   ");
 32                 System.out.print(rs.getString("name") + "   ");
 33             }
 34         }
 35         catch (SQLException e)
 36         {
 37             System.out.println("数据操作错误");
 38             e.printStackTrace();
 39         }
 40         finally
 41         {
 42             // 关闭数据库
 43             close(rs, stmt, conn);
 44         }
 45     }
 46     
 47     private static void driverLoader()
 48     {
 49         try
 50         {
 51             Class.forName("com.mysql.jdbc.Driver"); // 加载mysql驱动
 52         }
 53         catch (ClassNotFoundException e)
 54         {
 55             System.out.println("驱动加载错误");
 56             e.printStackTrace();
 57         }
 58     }
 59     
 60     private static Connection connectionGet()
 61     {
 62         Connection conn = null;
 63         try
 64         {
 65             conn = DriverManager.getConnection(url, user, password);
 66         }
 67         catch (SQLException e)
 68         {
 69             System.out.println("数据库链接错误");
 70             e.printStackTrace();
 71         }
 72         return conn;
 73     }
 74     
 75     private static void close(ResultSet rs, Statement stmt, Connection conn)
 76     {
 77         try
 78         {
 79             if (rs != null)
 80             {
 81                 rs.close();
 82                 rs = null;
 83             }
 84             if (stmt != null)
 85             {
 86                 stmt.close();
 87                 stmt = null;
 88             }
 89             if (conn != null)
 90             {
 91                 conn.close();
 92                 conn = null;
 93             }
 94         }
 95         catch (Exception e)
 96         {
 97             System.out.println("数据库关闭错误");
 98             e.printStackTrace();
 99         }
100     }
101 }
原文地址:https://www.cnblogs.com/moleme/p/4395732.html