Java中的BaseDao DBHelper超简单版,Java连接mysql数据库,操作mysql数据库

开发环境: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
本人所发布的博客中的所有资源文件一律免费

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小星博博

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值