开发环境:eclipse 2020-12
开发框架:JEE
开发语言:爪哇
1、先下载数据库连接的jdbc包(名称:mysql/mysql-connector-java.jar)
官方下载太慢,到下载:https://repo1.maven.org/maven2/mysql/mysql-connector-java/
开发工具包下载,推荐到 https://www.lmonkey.com/tools/java
进行下载,不是打广告,也不是我的网站,主要是下载快速
我用的版本是8.0.23
2、先建一个连接类DBConnection.java
package com.studentMange.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBConnection {
// 驱动类名
private static final String DRVERNAME = "com.mysql.cj.jdbc.Driver";
// 数据库地址
private static final String URL = "jdbc:mysql://localhost:3306/javadb";
// 账号
private static final String NAME = "xxxx";//填写连接数据库的账号
// 密码
private static final String PASSWORD = "123456";//填写连接数据库的密码
// 加载驱动
static {
try {
Class.forName(DRVERNAME);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 获取数据库连接
public static Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(URL, NAME, PASSWORD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
// 关闭连接,释放资源
public static void Close(ResultSet resultset, PreparedStatement preparedStatement, Connection connection) {
if (resultset!=null) {
try {
resultset.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
3、创建BaseDao.java类
package com.studentMange.db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
static Connection connection;
static PreparedStatement preparedStatement;
static ResultSet resultSet;
/*
* 说明:执行增删改 参数:(sql语句,n个替换占位符变量) 返回:boolean
*/
public static boolean ExecuteUpdate(String sql, Object... objects) {
connection = DBConnection.getConnection();
try {
preparedStatement = connection.prepareStatement(sql);
// 判断sql是否有?占位符,若存在则用循环替换掉
if (objects != null) {
for (int i = 0; i < objects.length; i++) {
try {
preparedStatement.setObject(i + 1, objects[i]);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 执行
try {
return preparedStatement.executeUpdate() > 0;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 关闭资源
finally {
DBConnection.Close(resultSet, preparedStatement, connection);
}
return false;
}
/*
* 说明:执行查询 参数:(sql语句,n个替换占位符变量) 返回:ResultSet
*/
public static ResultSet ExecuteQuery(String sql, Object... objects) {
connection = DBConnection.getConnection();
try {
preparedStatement = connection.prepareStatement(sql);
// 判断sql是否存在?占位符
if (objects != null) {
for (int i = 0; i < objects.length; i++) {
try {
preparedStatement.setObject(i+1, objects[i]);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 执行查询
try {
resultSet = preparedStatement.executeQuery();
return resultSet;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resultSet;
}
}
这样DBHelper就OK了,接下来就是调用了
查询
// 查询
public static List<Student> Select() {
List<Student> objs = new ArrayList<Student>();
String sql = "select * from student";
ResultSet res = BaseDao.ExecuteQuery(sql, null);
try {
while (res.next()) {
Student obj = new Student();
obj.setId(res.getInt("id"));
obj.setName(res.getString("name"));
obj.setSex(res.getString("sex"));
obj.setAge(res.getInt("age"));
objs.add(obj);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return objs;
}
public static Student SelectOne() {
String sql = "select * from student where Id=1";
ResultSet res = BaseDao.ExecuteQuery(sql);
if (res == null) {
return null;
}
Student obj = new Student();
try {
while (res.next()) {
obj.setId(res.getInt("id"));
obj.setName(res.getString("name"));
obj.setSex(res.getString("sex"));
obj.setAge(res.getInt("age"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return obj;
}
增加
// 增加
public static Boolean Add() {
Student student = new Student();
student.setName("李四2");
student.setSex("男");
student.setSex("25");
String sql = "INSERT INTO student VALUES(?,?,?)";
// ********************************(SQL语句,需要替换占位符的参数1,参数n...)
boolean re = BaseDao.ExecuteUpdate(sql, student.getName(), student.getSex(), student.getAge());
return re;
}
最后放一个案例目录截图
整合为的DBUtil工具类:
package test.com.xxbb.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
// 驱动类名
private static final String DRVERNAME = "com.mysql.cj.jdbc.Driver";
// 数据库地址
private static final String URL = "jdbc:mysql://localhost:3306/javadb";
// 账号
private static final String NAME = "xxxx";//填写连接数据库的账号
// 密码
private static final String PASSWORD = "123456";//填写连接数据库的密码
static Connection connection;
static PreparedStatement preparedStatement;
static ResultSet resultSet;
// 加载驱动
static {
try {
Class.forName(DRVERNAME);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 获取数据库连接
public static Connection getConnection() {
try {
connection = DriverManager.getConnection(URL, NAME, PASSWORD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
/*
* 说明:执行增删改 参数:(sql语句,n个替换占位符变量) 返回:boolean
*/
public static boolean ExecuteUpdate(String sql, Object... objects) {
try {
preparedStatement = connection.prepareStatement(sql);
// 判断sql是否有?占位符,若存在则用循环替换掉
if (objects != null) {
for (int i = 0; i < objects.length; i++) {
try {
preparedStatement.setObject(i + 1, objects[i]);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 执行
try {
return preparedStatement.executeUpdate() > 0;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 关闭资源
finally {
Close(resultSet, preparedStatement, connection);
}
return false;
}
/*
* 说明:执行查询 参数:(sql语句,n个替换占位符变量) 返回:ResultSet
*/
public static ResultSet ExecuteQuery(String sql, Object... objects) {
connection = getConnection();
try {
preparedStatement = connection.prepareStatement(sql);
// 判断sql是否存在?占位符
if (objects != null) {
for (int i = 0; i < objects.length; i++) {
try {
preparedStatement.setObject(i+1, objects[i]);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 执行查询
try {
resultSet = preparedStatement.executeQuery();
return resultSet;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resultSet;
}
// 关闭连接,释放资源
public static void Close(ResultSet resultset, PreparedStatement preparedStatement, Connection connection) {
if (resultset!=null) {
try {
resultset.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
最后附上源码下载链接 :https://download.csdn.net/download/baidu_39105563/14929566
本人所发布的博客中的所有资源文件一律免费