在JDBC中利用工具类来进行更新和查询

本文档展示了如何在Java中创建一个JDBC工具类(jdbcUtils),用于读取db.properties配置文件并建立数据库连接。同时,通过示例代码说明了如何使用该工具类进行数据库的插入和查询操作,包括executeUpdate()方法用于更新操作,executeQuery()方法用于查询操作,以及资源的释放方法release()。
摘要由CSDN通过智能技术生成

1在src中新建一个db.properties的file,并在文件中书写数据库的基本信息url,diver,username,password

 2新建jdbcUtils.java文件,进行书写工具类

package com.haoran.lesson2.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

//在该处写调用db.properties中关于连接目的数据库的基本信息
//在jdbcUtils类中进行配置文件的高内聚
public class jdbcUtils {
    private  static String driver = null;
    private  static String url = null;
    private  static String username = null;
    private  static String password = null;
    static {
        try{
            InputStream resourceAsStream = jdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
            //读取配置文件
            Properties properties = new Properties();
            properties.load(resourceAsStream);//下载资源
            driver=properties.getProperty("driver");
            url=properties.getProperty("url");
            username=properties.getProperty("username");
            password=properties.getProperty("password");

            //然后开始1,加载驱动
            Class.forName(driver);

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    //新建一个getConnection的方法类来实现 获取连接Connection 的功能
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,username,password);
    }



    //新建一个release的方法类来实现 释放连接资源 的功能
    public static void release(Connection conn, Statement st, ResultSet rs){
        if (rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (st!=null){
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

}

3然后进行数据库的更新和查询

更新:

package com.haoran.lesson2;

import com.haoran.lesson2.utils.jdbcUtils;

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

public class test {
    public static void main(String[] args) {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;

        try {
            conn=jdbcUtils.getConnection();//直接用工具类进行数据库连接
            st=conn.createStatement();//利用数据库连接conn获得SQL的执行对象st
            String sql="INSERT INTO `users`(id,`NAME`,`PASSWORD`,`email`,`birthday`)" +
                    "VALUE (4,'xiaozhang','1234','123123123@qq.com','1902-02-08')";
            //每次对数据库进行更新操作,只是该语句发生变化即可;
            int i = st.executeUpdate(sql);
            if (i>0){
                System.out.println("插入成功");
            }
            //当上述SQL语句为select查询语句时,st.executeQuery;
            // String sql="select * from users where id = 1";
//            rs =st.executeQuery(sql);
//            while(rs.next()){
//                System.out.println(rs.getString("NAME"));
//            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            jdbcUtils.release(conn,st,rs);
        }
    }
}

查询:

package com.haoran.lesson2;

import com.haoran.lesson2.utils.jdbcUtils;

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

public class testSelect {
    public static void main(String[] args) {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;

        try {
            conn= jdbcUtils.getConnection();//直接用工具类进行数据库连接
            st=conn.createStatement();//利用数据库连接conn获得SQL的执行对象st
            String sql="select * from users where id = 1";
            rs =st.executeQuery(sql);
            while(rs.next()){
                System.out.print(rs.getString("Name"));
            }
        } catch (
                SQLException e) {
            e.printStackTrace();
        }finally {
            jdbcUtils.release(conn,st,rs);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值