Java程序设计——数据库的访问优化(JDBC编程)

目录

1.编写属性文件

2.编写属性配置类

3.编写JDBCUtil工具类

4.实现类:


通过对数据库访问的基础代码的封装,可以简化访问数据库的操作,从而实现减少代码冗余以及提高效率

1.编写属性文件

在项目的根目录下新建一个config子目录,在子目录中创建一个jdbc.properties属性文件

 属性文件内容:

driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/DatabaseName?serverTimezone=UTC&useSSL=false
user = root
password = root

DataBaseName:数据库名字

数据库驱动管理类:8.0版本


2.编写属性配置类

属性配置类:

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class Config {
    private static Properties p = null;
    static {
        try {
            p = new Properties();    // 新建一个属性对象

            // 加载属性文件并通过输入流读取属性文件的信息
            p.load(new FileInputStream("config/jdbc.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //  获取属性文件中键对应的值
    public static String getValue(String key){
        return p.get(key).toString();
    }
}

3.编写JDBCUtil工具类

import java.sql.*;

public class JDBCUtil {
	private static Connection connection = null;
	private static PreparedStatement pstmt = null;
	private static ResultSet rs = null;

	// 获取数据库连接对象的静态方法
	public static Connection getConnection(){
		String driver = Config.getValue("driver");
		String url = Config.getValue("url");
		String user = Config.getValue("user");
		String password = Config.getValue("password");
		try {
			Class.forName(driver);
			connection = DriverManager.getConnection(url,user,password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}

	//	关闭相应的对象,释放资源
	public static void getClose(){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(pstmt!=null){
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(connection!=null){
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	//	查询语句
	public static ResultSet excuteQuery(String sql, Object[] objects){
		try {
			pstmt = connection.prepareStatement(sql);
			for (int i = 0; i < objects.length; i++) {
				pstmt.setObject(i + 1, objects[i]);
			}
			rs = pstmt.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	//	插入语句——更新语句——删除语句
	public static int excuteUpdate(String sql, Object[] objects){
		int count = 0;
		try {
			pstmt = connection.prepareStatement(sql);
			for (int i = 0; i < objects.length; i++) {
				pstmt.setObject(i + 1, objects[i]);
			}
			count = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
}

4.实现类:

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

public class MainClass {
    public static void main(String[] args) throws SQLException {
        JDBCUtil.getConnection();
        String sql = "SELECT * FROM tb_student WHERE sex=? AND nation=?";
        Object[] objects = new Object[]{"男","汉"};
        ResultSet rs = JDBCUtil.excuteQuery(sql,objects);
        while(rs.next()){
            System.out.print(rs.getInt(1)   +"\t");
            System.out.print(rs.getString(2)+"\t");
            System.out.print(rs.getString(3)+"\t");
            System.out.print(rs.getString(4)+"\t");
            System.out.print(rs.getString(5)+"\t");
            System.out.print(rs.getString(6)+"\t");
            System.out.print(rs.getString(7)+"\t");
            System.out.println();
        }
        JDBCUtil.getClose();
    }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

来得晚一些也行

观众老爷,请赏~

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

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

打赏作者

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

抵扣说明:

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

余额充值