JDBC访问数据库方法

JDBC访问各种类型数据库的前提:
由于不同数据库产品的实现方式通信协议不一样,所以各数据库厂商以jar包(数据库驱动)的形式提供实现类,jar包含有已经写好的类和接口,只需要将其引入项目中就可直接使用。

jar包的引入:

  1. 首先要使用什么数据库就下载什么数据库的jar包,以MySQL举例,直接进入MySQL官网下载即可https://dev.mysql.com/downloads/connector/j/
  2. 将下载好的jar包复制到项目文件夹中
  3. 在eclipse中右键点击jar包选择Build Path中的Add to Build Path就可以将其引入项目中了
    在这里插入图片描述

访问流程:

  1. 注册驱动:使用Class.forName()加载上一步引入的jar包中的驱动类,要用try包住。
    若出现报错

Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

表明数据库驱动com.mysql.jdbc.Driver’已经被弃用了,应当使用新的驱动com.mysql.cj.jdbc.Driver’

try {
	Class.forName("com.mysql.cj.jdbc.Driver") ;//注册驱动
} 
catch (ClassNotFoundException e) {}
  1. 获取连接:DriverManager用于管理数据库驱动,建立与数据库的连接;Connection用于表示与数据库的连接。
    格式:DriverManager.getConnection(“jdbc:mysql://IP地址:端口/数据库名字”, 用户名, 密码) ,同样要用try包住
    MySQL默认端口为3306,若服务器在本机可用localhost或127.0.01
    java连接时如果出现异常

The server time zone value ‘�й���׼ʱ��’ is unrecogni

是因为使用了Mysql Connector/J 6.x以上的版本,然后就报了时区的错误。加入参数serverTimezone=UTC就好了。

try {	//获取连接
	Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8", "userName", "password")
} 
catch (SQLException e) {}
  1. 执行操作语句:Statement用于操作sql语句,首先要获取Statement
Statement stmt = con.createStatement() ;

然后调用executeUpdate()方法,sql语句用字符串的形式作为参数,该方法是int类型的,会返回一个受影响的行数相当于执行完插入语句返回的语句

String sql = "insert into t_user(username,password,age) values('aaa', '789', 22);" ;
stmt.executeUpdate(sql) ;

在这里插入图片描述
若使用sql语言出现中文乱码,则要在连接数据库的时候再添加参数useUnicode=true&characterEncoding=utf8
4. 处理结果:接上一步的executeUpdate()方法会返回一个值,可将其输出查看结果
5. 关闭资源:访问完数据库之后不能一直占用通道,需要将其关闭,关闭的顺序要与打开的顺序相反,先关Statement,再关Connection,也要用try包住

if(stmt != null) {	//先关Statement
	try {
		stmt.close() ;
	} 
	catch (SQLException e) {}
}
if(con != null) {	//再关Connection
	try {
		con.close() ;
	} 
	catch (SQLException e) {}
}

基本流程例子:

package mysql访问;
import java.sql.*;
public class 操作流程 {
	public static void main(String[] args) {
		String driverClassName = "com.mysql.cj.jdbc.Driver" ;
		String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8" ;
		String userName = "root" ;
		String password = "不能给你们知道" ;
		Connection con = null ;
		Statement stmt = null ;
		try {
			//注册驱动
			Class.forName(driverClassName) ;	
			//获取连接
			con = DriverManager.getConnection(url, userName, password ) ;	
			//获取Statement
			stmt = con.createStatement() ;
			//执行sql语句
			String sql = "insert into t_user(username,password,age) values('小明', '789', 22);" ;
			int num = stmt.executeUpdate(sql) ;
			//处理结果
			System.out.println(num) ;
		} 
		catch (ClassNotFoundException e) {System.out.println(e) ;}
		catch (SQLException e) {System.out.println(e) ;}
		finally {	//关闭资源
			if(stmt != null) {	//先关Statement
				try {
					stmt.close() ;
				} 
				catch (SQLException e) {}
			}
			if(con != null) {	//再关Connection
				try {
					con.close() ;
				} 
				catch (SQLException e) {}
			}
		}
	}
}

总结:

  • JDBC相关的类和接口都在java.sql中
作用
DriverManager 管理数据库驱动
Connection 表示与数据库的连接
Statement 执行SQL语
方法 作用
Class.forName() 加载类,可注册驱动
DriverManager.getConnection() 建立与数据库的连接
con.createStatement() 获取Statement
stmt.executeUpdate() 进行sql操作
stmt.close() 关闭Statement
con.close() 关闭Connection
发布了25 篇原创文章 · 获赞 71 · 访问量 8663

猜你喜欢

转载自blog.csdn.net/weixin_44689154/article/details/104383553