目录
controlle:作用与前端交互
如:获取前端数据,给前端响应数据
sercer:作用处理业务
dao:作用操作数据库
建立项目时可以创建这几个包
controlle:前端相关
server:业务处理 如:main
dao:操作数据库 如:使用DQL,DML操作语句
bean:对应实体 如:用户类
utils:工具类 如:配置文件,封装的数据库连接
在编写代码时需要重复的连接关闭数据库,导致了大量代码冗余,此时可以将其封装到一个类中,并提供静态方法
从上个JDBC1博客看例子,Connection对象需要重复使用,关闭也需要重复关闭,而配置代码也写在了此代码中,将其全部分开,可以使代码更加简洁。
将其写进一个 MyconnectionUtils类
- import java.io.IOException;
- import java.io.InputStream;
- import java.sql.*;
- import java.util.Properties;
-
- public class MyconnectionUtils {
-
- static {//静态只在类加载时加载一次
- try {
- Class.forName("com.mysql.jdbc.Driver");
- }catch (ClassNotFoundException e){
- e.printStackTrace();
- }
- }
-
- public static Connection getConnection() throws IOException, SQLException {
- //提供connection对象
- InputStream is =MyconnectionUtils.class.getResourceAsStream("Dbinfo.properties");
- //输入流,读取根目录下的Dbinfo.properties
- Properties properties = new Properties();//使用Properties来读取配置内容
- //Properties 可以读取字典内容,如下相当于用键读值
- properties.load(is);
- String url = properties.getProperty("URL");
- String username = properties.getProperty("USER");
- String password = properties.getProperty("PASSMI");
-
- Connection connection = DriverManager.getConnection(url, username, password);
- return connection;//返回connection
- }
-
- public static void Close(ResultSet resultSet,Statement statement, Connection connection) throws SQLException {
- //用来关闭
- if (resultSet != null && ! resultSet.isClosed()){
- resultSet.close();//关闭结果
- }
- if (statement != null && !statement.isClosed()){
- statement.close();//关闭提交
- }
- if (connection != null && !connection.isClosed()){
- connection.close();//关闭连接
- }
- }
- }
Dbinfo.properties 文件内容:

使用:使用时直接用类名调用其静态方法
- import java.io.IOException;
- import java.sql.*;
- public class Main {
- public static void main(String[] args) {
- Connection connection = null;
- try {
- connection = MyconnectionUtils.getConnection();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- try {
- connection.setAutoCommit(false);//关闭自动提交
- String sql1="update yinhang set u_age=u_age+10 where u_id= ?";
-
- PreparedStatement statement = connection.prepareStatement(sql1);
- statement.setInt(1,1);
- statement.executeUpdate();
-
- String sql2="update yinhang set u_age=u_age-10 where u_id= ?";
- PreparedStatement statement2 = connection.prepareStatement(sql2);
- statement2.setInt(1,2);//
- statement2.executeUpdate();
-
- connection.commit();//提交执行
-
- MyconnectionUtils.Close(null,statement,null);
- MyconnectionUtils.Close(null,statement2,connection);//关闭资源
-
- } catch (Exception e) {
- try {
- connection.rollback();//出现任何错误直接回滚,保证数据安全
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }finally {
- try{
- if(connection != null&& connection.isClosed()){
- connection.setAutoCommit(true);//变回自动提交
- }
- }catch (SQLException e){
- e.printStackTrace();
- }
-
- }
- }
- }
getResourceAsStream方法
如果最前面是正斜杆/,那么则从项目根目录开始查找
如果不加正斜杆/,那么则从该方法的调用者class所在的目录查找。
作用:存储与管理数据库连接,帮助我们创建,销毁复用数据库连接
综合能Druid(德鲁伊)最好
Druid连接池是阿里巴巴开源的数据库连接池项目
1,下载Druid的jar包
2,导入jar包
3,编写代码
加载配置文件
获取数据库连接池对象
获取连接
配置文件

-
- public class Myconnest {
- static {//静态只在类加载时加载一次
- try {
- Class.forName("com.mysql.jdbc.Driver");
- }catch (ClassNotFoundException e){
- e.printStackTrace();
- }
- }
- public static Connection getConnection() throws Exception {
- InputStream is = Myconnest.class.getResourceAsStream("druid.properties");
- Properties properties = new Properties();
- properties.load(is);
-
- DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
- Connection connection = dataSource.getConnection();
-
- return connection;
- }
- }
此连接池会自动连接,会自动关闭
试试效果
- public class Test {
- public static void main(String[] args) throws Exception {
-
- Connection connection = Myconnest.getConnection();
- String sql="select u_id,u_username,u_mima,u_sex,u_age from yinhang";
- Statement statement = connection.createStatement();
- ResultSet set = statement.executeQuery(sql);
-
- while (set.next()){
- String str1 = set.getString("u_username");
- System.out.println(str1);
- }
-
- }
- }

QueryRunner(DruidDataSource);//可传入DruidDataSource
作用:执行DQL查询语句,类似与executeQuery方法
query(连接对象,SQl,ResultSetHandler<定义的类>(),可变参数填sql?对应的值)
作用:执行DML修改语句,类似与executeUpdate方法
update(sql,值)
返回值为影响行数
- String sql = "INSERT INTO my_user (m_user,m_password,m_name,m_sxe,m_age,m_img)VALUES(?,?,?,?,?,?)";
- QueryRunner queryRunner = new QueryRunner(MyconnectionUtils.getDataSource());
- int num = queryRunner.update(sql,user.getM_user(),user.getM_password(),user.getM_name(),user.getM_sxe(),user.getM_age(),user.getM_img());
-
-
-
- //插入语句
ResultSetHandler:接口
具体查询:
BeanHandler:返回一个对象
注意: 1,bean类必须提供无参构造函数,如果没有无参构造会报错
2,bean类的属性名要与查询的结果的列名一致,如果名称不
一致可 能会取不到值
- String sql = "SELECT m_id,m_user,m_password,m_name,m_age,m_money,m_img FROM my_user WHERE m_user = ? ";
- QueryRunner queryRunner = new QueryRunner(MyconnectionUtils.getDataSource());
- BeanHandler<User> userBeanHandler = new BeanHandler<>(User.class);
-
- User user = queryRunner.query(sql,userBeanHandler,username);//返回一行
BeanListHandler:返回一个集合 注意:
1,bean类必须提供无参构造函数,如果没有无参构造会报错
2,bean类的属性名要与查询的结果的列名一致,如果没有无
参构造 会报错
- String sql="SELECT * FROM student";
- QueryRunner queryRunner = new QueryRunner(MyconnectionUtils.getDataSource());
- BeanListHandler<Student> listHandler = new BeanListHandler<>(Student.class);
-
- List<Student> query = queryRunner.query(sql, listHandler);//返回list
- //返回了多个
ScalarHandler:返回一个值