JDBC原理及访问SqlServer数据库的操作

1.JDBC 全称: Java DataBase Connectivity

实现机制
JDBC原理及访问SqlServer数据库的操作_第1张图片

2.JDBC API 功能的具体实现方法

     Drivermanager : 加载管理驱动
     Connection : 建立连接  (通过DriverManager产生)
     Statement (PreparedStatement) :增删改查 (通过Connection产生)
     CallableStatement :调用存储函数 (通过Connection产生)
     Result : 返回结果集 (通过上面的Statement等产生)

(Connection)

Connection产生Statement对象:connection.createStatement(String sql)
Connection产生PreparedStatement对象:connection.prepareStatement(String sql)   //注意对象和方法名  有个 'd'  的区别
Connection产生CallableStatement对象:connection.prepareCall(String sql)

(Statement)

增删改:executeUpdate()
查询:executeQuery()

(PreparedStatement)

public interface PreparedStatement extends Statement       //Statement 的子接口
增删改:executeUpdate()
查询:executeQuery()
赋值操作: setxxx()   //setInt() , setLong() , setString(),,,,,

(CallableStatement)

String sql = "exec 过程名  ?(参数)"  
connection.prepareCall(sql)
给参数赋值 cstmt.setInt(1, 21); 
ResultSet  rs = cstmt.executeQuery();  //执行,返回结果集

( ResultSet)

next();  光标下移  有数据为true  否则为False
getxxx();获取具体的字段值(属性名  或  属性下标(从1开始))

3.JDBC访问数据库的具体步骤

JDBC原理及访问SqlServer数据库的操作_第2张图片
a. 导包 加载具体的驱动类
     网上下载对应数据库的驱动jar包 我的数据库是SqlServer下载好后,将jar包添加到项目中(可以直接复制)。
     然后右键点击Build Path 添加进去:
在这里插入图片描述
     打开jar包,找到驱动类,右键点击Copy 路径:
JDBC原理及访问SqlServer数据库的操作_第3张图片
     加载驱动类:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 加载具体的驱动类

b. 与数据库建立连接

    private static final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=test";
	private static final String USERNAME = "abc";
	private static final String PWD = "123";
	...
	connection = DriverManager.getConnection(URL, USERNAME, PWD);
    ...

c. 发送SQL语句执行

String sql = "insert into stu values('2020',‘张三','男',19)";
//String sql = "update stu set sno = '2021'  where sname = '张三'";
//String sql = "delete from stu where sname = '张三'";
pstmt = connection.prepareStatement(sql);
int count = pstmt.executeUpdate();
			

d. 处理结果集
这里就简单打印说明一下:

if (count>0) {
				System.out.println("操作成功");
			}

使用JDBC操作数据库时,如果对数据库进行了更换,只需要替换:驱动,具体驱动类,连接字符串,用户名,密码。

4.源代码

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class jdbcprestament {

	private static final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=test";
	private static final String USERNAME = "abc";
	private static final String PWD = "123";

	public static void update() {// 增删改
		Connection connection = null;
		PreparedStatement pstmt = null;
		try {

//		a.导入驱动,加载具体的驱动类		
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 加载具体的驱动类

//		b.与数据库建立连接
			connection = DriverManager.getConnection(URL, USERNAME, PWD);

//		c.发送sql语句,执行命令(增删改,查)	
		String sql = "insert into stu values('2020',‘张三','男',19)";
		//String sql = "update stu set sno = '2021'  where sname = '张三'";
		//String sql = "delete from stu where sname = '张三'";
			pstmt = connection.prepareStatement(sql);
		    int count = pstmt.executeUpdate();
			
			
//		d.处理结果集
			if (count>0) {
				System.out.println("操作成功");
			}
		} catch (ClassNotFoundException e) { // 第一个异常
			e.printStackTrace();
		} catch (SQLException e) { // 第二个异常
			e.printStackTrace();
		} catch (Exception e) { // 保险起见,再写一个根异常
			e.printStackTrace();
		} finally {
			try {
				if(pstmt != null) pstmt.close(); //防止空指针异常,判断一下再关闭
				if(connection != null) connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}

	public static void query() {  //查询
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {

//		a.导入驱动,加载具体的驱动类		
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 加载具体的驱动类

//		b.与数据库建立连接
			connection = DriverManager.getConnection(URL, USERNAME, PWD);

//		c.发送sql语句,执行命令(增删改,查)					
			String sql = "select * from stu where sname='张三'"; 
			pstmt = connection.prepareStatement(sql); 
			rs = pstmt.executeQuery();
			
//		d.处理结果集
			while(rs.next()) {
				String sno =	rs.getString("sno");
				String sname = rs.getString("sname");	
				System.out.println(sno+sname);
			}
			
		} catch (ClassNotFoundException e) { // 第一个异常
			e.printStackTrace();
		} catch (SQLException e) { // 第二个异常
			e.printStackTrace();
		} catch (Exception e) { // 保险起见,再写一个根异常
			e.printStackTrace();
		} finally {
			try {                              //关闭顺序:先开的后关,类似于栈
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close(); //防止空指针异常,判断一下再关闭
				if(connection != null) connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	} 
	public static void main(String args[]) {
	//update();
		query();
	}
	
}

你可能感兴趣的:(JDBC原理及访问SqlServer数据库的操作)