Jdbc初体验

Java数据库连接(JDBC)由一组用 Java 编程语言编写的类和接口组成。JDBC 为工具/数据库开发人员提供了一个标准的 API,使他们能够用纯Java API 来编写数据库应用程序。然而各个开发商的接口并不完全相同,所以开发环境的变化会带来一定的配置变化。本文介绍的是连接SQLServer数据库:

1.打开SQLServer的配置管理器:

2.打开TCP/IP的属性,IPALL的端口改为1433

3.打开服务,重新启动SQL Server(SQLEXPRESS)

4.下面开始写简易的JDBC:

String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=bookDB";
String USERNAME = "sa";
String PASSWORD = "1234";

 

 1 package com.ccec.jdbc;
 2 
 3 import java.beans.Statement;
 4 import java.sql.Connection;
 5 import java.sql.DriverManager;
 6 import java.sql.PreparedStatement;
 7 import java.sql.ResultSet;
 8 import java.sql.SQLException;
 9 
10 public class jdbc {
11     private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
12     private static final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=bookDB";
13     private static final String USERNAME = "sa";
14     private static final String PASSWORD = "1234";
15 
16     public boolean login(String username, String password) {
17         Connection con = null;
18         PreparedStatement st = null;
19         ResultSet rs = null;
20 
21         try {
22             Class.forName(DRIVER);
23             con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
24             String sql = "select userName,pwd from userInfo where userName=? and pwd=?";
25             st = con.prepareStatement(sql);
26             st.setString(1, username);
27             st.setString(2, password);
28             rs = st.executeQuery();
29 
30             if (rs.next()) {
31                 System.out.println(username);
32                 return true;
33             }
34         } catch (ClassNotFoundException e) {
35             // TODO Auto-generated catch block
36             e.printStackTrace();
37         } catch (SQLException e) {
38             // TODO Auto-generated catch block
39             e.printStackTrace();
40         } finally {
41             try {
42                 if (rs != null) {
43                     rs.close();
44                 }
45                 if (st != null) {
46                     st.close();
47                 }
48                 if (con != null) {
49                     con.close();
50                 }
51             } catch (SQLException e) {
52                 // TODO Auto-generated catch block
53                 e.printStackTrace();
54             }
55         }
56         return false;
57     }
58 }

 5.封装后的JDBC

  1 package com.ccec.jdbc;
  2 
  3 /**
  4  * @author StayReal
  5  */
  6 import java.sql.Connection;
  7 import java.sql.DriverManager;
  8 import java.sql.PreparedStatement;
  9 import java.sql.ResultSet;
 10 import java.sql.SQLException;
 11 
 12 public class jdbc {
 13     // SQLServer驱动类的全名(包名.;类名)
 14     private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
 15     // 连接的URL
 16     private static final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=bookDB";
 17     // 登陆SQLServer的用户名
 18     private static final String USERNAME = "sa";
 19     // 登陆SQLServer的密码
 20     private static final String PASSWORD = "1234";
 21     // 数据库的对象声明
 22     private Connection con = null;
 23     private PreparedStatement pst = null;
 24     private ResultSet rs = null;
 25 
 26     /**
 27      * 加载驱动,建立连接
 28      * 
 29      * @throws ClassNotFoundException
 30      * @throws SQLException
 31      */
 32     private void getConnection() throws ClassNotFoundException, SQLException {
 33         Class.forName(DRIVER);
 34         con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
 35     }
 36 
 37     /**
 38      * 执行查询
 39      * 
 40      * @param sql
 41      *            执行的SQL语句
 42      * @param params
 43      *            Object数组 封装所有的SQL语句参数 顺序与SQL语句是参数顺序一致
 44      * @return ResultSet 返回执行的结果
 45      */
 46     public ResultSet execQuery(String sql, Object[] params) {
 47         try {
 48             getConnection();
 49             pst = con.prepareStatement(sql);
 50             setPrepareStatementParams(params);
 51             rs = pst.executeQuery();
 52         } catch (SQLException e) {
 53             e.printStackTrace();
 54         } catch (ClassNotFoundException e) {
 55             e.printStackTrace();
 56         }
 57         return rs;
 58     }
 59 
 60     /**
 61      * 执行增加、删除、修改SQL操作的方法
 62      * 
 63      * @param sql
 64      *            执行的参数化SQL语句
 65      * @param params
 66      *            Object数组 封装所有的SQL语句参数 顺序与SQL语句是参数顺序一致
 67      * @return int型 受影响的行数 -1表示出现异常
 68      */
 69     public int execUpadte(String sql, Object[] params) {
 70         try {
 71             getConnection();
 72             pst = con.prepareStatement(sql);
 73             setPrepareStatementParams(params);
 74             int affectedRows = pst.executeUpdate();
 75             return affectedRows;
 76         } catch (ClassNotFoundException e) {
 77             e.printStackTrace();
 78         } catch (SQLException e) {
 79             e.printStackTrace();
 80         } finally {
 81             closeAll();
 82         }
 83         return -1;
 84     }
 85 
 86     /**
 87      * 为PrepareStatement设置参数
 88      * 
 89      * @param params
 90      *            参数数组
 91      * @throws SQLException
 92      */
 93     private void setPrepareStatementParams(Object[] params) throws SQLException {
 94         if (params != null) {
 95             // 传一个数组的参数
 96             for (int i = 0; i < params.length; i++) {
 97                 pst.setObject(i + 1, params[i]);
 98             }
 99         }
100     }
101 
102     /**
103      * 关闭Connection ,PrepareStatement,ResultSet
104      */
105     private void closeAll() {
106         try {
107             if (rs != null) {
108                 rs.close();
109             }
110             if (pst != null) {
111                 pst.close();
112             }
113             if (con != null) {
114                 con.close();
115             }
116         } catch (SQLException e) {
117             e.printStackTrace();
118         }
119     }
120 }

 

posted @ 2014-08-13 15:19  最重要的小事  阅读(297)  评论(0编辑  收藏  举报