您的位置:首页 > 博客中心 > 数据库 >

详解连接SQL Server数据库的方法,并使用Statement接口实现对数据库的增删改操作

时间:2022-03-14 13:58

总结一下,连接SQL Server数据库需要以下几个步骤:

1. 导入驱动Jar包:sqljdbc.jar

2. 加载并注册驱动程序

3. 设置连接路径

4. 加载并注册驱动

5. 连接数据库

6. 操作数据库

7. 关闭连接

代码如下:

******************连接数据库*******************

 1 package zj6_Test;
 2 import java.sql.*;
 3 public class Zj6_3 {
 4     /**
 5      *   使用Statement接口实现对数据库的增删改操作
 6      */
 7     private static final String driver=
 8             "com.microsoft.sqlserver.jdbc.SQLServerDriver";//加载并注册驱动程序
 9     private static final String url=
10             "jdbc:sqlserver://localhost:1433;DataBaseName=Bank";//设置连接路径
11     public static void main(String[] args) {
12         Connection con=null;//建立连接池
13         Statement sta=null;
14         try {
15             Class.forName(driver);//加载并注册驱动
16             con=DriverManager.getConnection(url, "sa", "sa");//连接数据库
17 //                向数据库Bank表中添加数据
18             sta=con.createStatement();//通过createStatement()方法得到Statement接口的引用指向的对象
19             sta.execute("insert into ACCOUNT values(‘曹操‘,‘420106198205188777‘,‘2011-07-07‘)");
20             sta.close(); //关闭Ststement对象
21 //            //修改表中ACCOUNT_ID为7的数据
22 //            String ACCOUNT_NAME="曹植";
23 //            String code="420683199203212111";
24 //            String OPEN_TIME="2011-07-10";
25 //            sta=con.createStatement();
26 //            String updatesql="update ACCOUNT set ACCOUNT_NAME=‘"+ACCOUNT_NAME+"‘,code=‘"+code+"‘,OPEN_TIME=‘"+OPEN_TIME+"‘where ACCOUNT_ID="+7 ;
27 //                
28 //            sta.execute(updatesql);
29 //            sta.close();
30 //            //删除ACCOUNT表中ACCOUNT_ID=7的记录
31 //            sta=con.createStatement();
32 //            String delsql="delete from ACCOUNT where ACCOUNT_ID="+7;
33 //            sta.executeUpdate(delsql);
34 //            sta.close();
35 //            con.close();            
36         } catch (Exception e) {
37             e.printStackTrace();
38         }    
39     }
40 }

运行结果:

技术分享

当然,实际应用中,为了体现java封装的特性,往往会把重复使用的方法封装到一个类中,每次直接拿来用就可以了。

下面给一个封装的类:

 1 package zj6_Test;
 2 import java.sql.*;
 3 public class DBManager {
 4     /**
 5      *  建立专门的自定义类,来实现建立连接、关闭对象和关闭连接
 6      */
 7     private static final String driver=
 8             "com.microsoft.sqlserver.jdbc.SQLServerDriver";
 9     private static final String url=
10             "jdbc:sqlserver://localhost:1433;DataBaseName=Bank";
11         private static final String user="sa";
12         private static final String pwd="sa";
13         private static Connection con=null;//建立连接池对象
14         //建立与数据库的连接
15         public static Connection getCon(){
16             try {
17                 Class.forName(driver);
18                 con=DriverManager.getConnection(url,user,pwd);
19             } catch (Exception e) {
20                 
21                 e.printStackTrace();
22             }
23             return con;    
24         }
25         //关闭Connection
26         public static void closeCon(Connection con){
27             try {
28                 if(con!=null){
29                     con.close();
30                 }
31             } catch (SQLException e) {
32                 e.printStackTrace();
33             }
34         }
35         //关闭ResultSet
36         public static void closeResultSet(ResultSet rst){
37             try {
38                 if(rst!=null){
39                     rst.close();
40                     rst=null;
41                 }
42             } catch (Exception e) {
43                 e.printStackTrace();
44             }
45         }    
46         //关闭Statement
47         public static void closeStatement(PreparedStatement pst){
48             try {
49                 if(pst!=null){
50                     pst.close();
51                     pst=null;
52                 }
53             } catch (Exception e) {
54                 e.printStackTrace();
55             }
56         }
57     }

能力有限,有些地方地方说法不够专业,还望批评指正!

本类排行

今日推荐

热门手游