使用JDBC连接、操作数据库、实现数据处理

JDBC:java database connection (java和 数据库的连接 ) 就是使用java代码来操作数据库。
接下来就来看一下具体的操作吧,数据库名test。

JDBC操作数据库的步骤:

1)加载数据库驱动。

2)连接数据库。

3)定义SQL语句。(这里我是用测试类完成的)

4)执行数据库操作。

5)关闭对象,回收数据库资源。
package com.framework.model.util;

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

public class DBUtil {
	private Connection conn = null;
	private Statement sta = null;
	private ResultSet rs = null;
		
	private Connection connectionToDB() {
		Connection myConn = null; 
		//增加了代码的复用性
		try {
			//加载数据库驱动
			//旧的过期驱动 : com.mysql.jdbc.Driver
			//新提供的驱动类 : com.mysql.cj.jdbc.Driver
			Class.forName("com.mysql.cj.jdbc.Driver");
			//获取数据库连接
			//其中,useSSL=FALSE:MySQL在高版本需要指明是否进行SSL连接
			//serverTimezone=UTC: UTC代表的是全球标准时间 ,但是我们使用的时间是北京时区也就是东八区,领先UTC八个小时。UTC + (+0800) = 本地(北京)时间
			String url = "jdbc:mysql://localhost:3306/test?&useSSL=FALSE&serverTimezone=UTC";
			myConn = DriverManager.getConnection(url, "root", "");
			System.out.println("数据库连接成功!");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
		return myConn;
	}
	
	public DBUtil() {
		this.conn = this.connectionToDB();
	}
	
	public int update(String sql) {
		int n = -1;
		try {
			this.sta = this.conn.createStatement();
			n = this.sta.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}
	
	public ResultSet query(String sql) {
		try {
			this.sta = this.conn.createStatement();
			this.rs = this.sta.executeQuery(sql);
			return this.rs;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this.rs;
	}
	
	//关闭数据集
	public void close() {
		try {
			if(this.rs != null) {
				this.rs.close();//关闭记录集
				this.rs = null;
			}
			if(this.sta != null) {
				this.sta.close();// 关闭数据库操作对象
				this.sta = null;
			}
			if(this.conn != null) {
				this.conn.close();// 关闭数据库连接对象
				this.conn = null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

JDBC测试数据库连接:

package com.framework.model.util;

import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtilTest {
	
	public static void main(String[] args) {
		DBUtil dbUtil = new DBUtil();
		
		//增、删、改代码
		String sql = "delete from test where Id = '1'";
		int n = dbUtil.update(sql);
		if(n > 0) {
			System.out.println("删除成功!");
		}
		
		//查询代码
		String sql = "select * from test";
		ResultSet rs = dbUtil.query(sql);
		try {
			while(rs.next()){
				System.out.print(rs.getString(1) + " ");
				System.out.print(rs.getString(2) + " ");
				System.out.println(rs.getString(3));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			dbUtil.close();
		}
	}
}

希望对你测试有用~~~

原文地址:https://www.cnblogs.com/Indomite/p/14195268.html