JDBC

一、什么是JDBC?

                   java中连接数据库的一种技术

是java各种应用程序和数据库之间的桥梁

由一组使用java语言编写的类和接口组成

二、JDBC中常用的API?

l  DriverManager类:管理不同数据库的jdbc驱动

l  Connection接口:负责连接数据库并担任传递数据的任务

l  Statement接口:由Connection产生,负责执行sql语句

n  PreparedStatement是Statement的子接口

n  除了具备父接口Statement的能力外,还具有4高(安全性、性能、可读性、可维护性)功能

l  ResultSet接口:负责保存和处理Statement返回的查询结果

三、使用JDBC如何连接sqlserver数据库?

         1、加载驱动

                            编码前的准备工作

  1.将sqljdbc2008.jar文件复制粘贴到项目的文件夹中

  2.将项目和jdbc驱动关联

 

 

         编码,加载驱动

                   Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);//处理异常try…catch

2、编写数据库连接字符串、设置登录名、密码

                   2.1   final String url = ”jdbc:sqlserver://localhost:1433;

databasename=存在的数据库名”;

                   2.2   final String name = ”sqlserver身份验证的登录名”;

                   2.3   final String pwd = ”登录名对应的密码”;

3、使用DriverManger类的getConnection()方法,

关联url、name、pwd,返回一个Connection接口

          Connection conn=DriverManger.getConnection(url,name,pwd);

  

四、使用Statement接口对表的数据执行[增、删、改]操作

1、加载驱动连接数据库

2、  在try…catch外面,声明Statement变量并赋初值null

                   (或用PreparedStatement代替Statement

3、  定义变量,保存sql语句(insert、delete、update语句)

使用PreparedStatement接口,sql语句中values()中的值,使用?

4、  DriverManager.getConnection()的下一行编写代码

Statement变量=Connection变量.createStatement();

PreparedStatement变量= Connection变量. prepareStatement(sql);

5、调用Statement变量的executeUpdate(sql)执行sql语句

                            int rows=executeUpdate(sql);//返回受影响的行数

sql语句中的?赋值

                  PreparedStatement变量.set数据类型(数字,值);

调用PreparedStatement变量的executeUpdate()执行sql语句

                   int rows=executeUpdate();//返回受影响的行数

 

6、根据受影响的行数,判断sql语句执行是否成功

7、释放相关的资源

finally{

                    try{

                            if(Statement变量或PreparedStatement变量!=null)

                                     Statement变量(或PreparedStatement变量).close();

                            if(Connection变量!=null)

                                    Connection变量.close();

                    }catch(SQLException e){

                            e.printStackTrace();

                    }

五、使用JDBC查询表中的数据(ResultSet接口)

1、加载sql2008 jdbc驱动包

(自动添加try…catch,手动添加finally语句块

用于释放资源)

3、  在try…catch…finally语句块的外面,

a)         声明3个接口(Connection、Statement或PreparedStatement、ResultSet的变量且赋值null

b)         在finally块中,调用这3个接口的close()释放资源

4、  编写数据库连接字符串url

5、  使用DriverManager.getConnection(url,”登录名”,”登录密码”);

获取Connection接口的对象

6、  编写sql查询语句,保存到字符串的变量中

7、  获取发送并执行sql查询语句的接口对象

a)         父接口Statement对象

Connection接口变量.createStatement();

b)         子接口PreparedStatement对象

Connection接口变量. prepareStatement(sql);

8、  调用Statement或PreparedStatement接口的方法,

执行sql查询语句,返回ResultSet

a)                  父接口Statement对象

ResultSet变量=Statement接口变量.executeQuery(sql);

b)         子接口PreparedStatement对象

ResultSet变量=Statement接口变量.executeQuery();

9、  使用while循环,调用ResultSet接口变量的

get数据类型(数字或列名)方法,来获取表中列的值

while(ResultSet接口变量.next()){

           ResultSet接口变量.get数据类型(数字或列名);

                    get数据类型(数字);//数字从1开始

                    get数据类型(“列名”);

}

原文地址:https://www.cnblogs.com/songjiabo/p/6825909.html