JDBCUtil的使用

为什么使用JDBCUtil?

以前写jdbc的代码的时候,没写一个jdbc代码就要重新写一遍注册数据库,预计数据库获得链接,还有最后的关闭相关的对象。

这样写效率不高,也不利于以后的更改。比如说:要更改数据库的时候,就要一个个的更改过去,不方便。这里抽取出相关的方法,并且把与数据库相关的参数(比如说是:数据库名,用户名,密码等)放在一个配置的文件中,以后要更改,就直接更改配置文件就可以了。

1.

首先,在src的目录下创建一个配置文件:db.propertices

里面可以写入:

driver:com.mysql.jdbc.Driver

url:jdbc:mysql://localhost:3306/user

username:root

password:root

这样就配置完成。

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/user
username=root
password=root

2.

抽取出相关的代码变成方法:

在之前写的查询或者登陆的代码中,选中注册到获得链接的代码,在idea中,右键->refactor->Export->method,这样就可以抽取出相关的方法。同理在关闭相关的对象的代码中叶抽取出方法。

3.在src下创建一个util的包,里面创建一个JDBCUtil的工具类。

把刚才抽取的方法放在这里。

还要处理:从db.propertices中获得相关对象的值。

在静态代码块中操作,这样可以直接加载。

这里的变量是全局静态变量,这样下面的方法可以直接使用。在idea中设置全局变量的快捷方式为:ctrl+alt+f。

还要处理相关的异常,这里异常不要向上抛,全部用try—catch来在方法中捕捉。

另外,在关闭相关对象的方法中,要先判断相关的对象是否为空。

package net.zixue.utils;

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

public class JDBCUtil {
    static {
        ClassLoader classLoader = JDBCUtil.class.getClassLoader();
        InputStream resourceAsStream = classLoader.getResourceAsStream("db.properties");
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");

    }

    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    public static Connection getConnection()  {
        //Driver driver;
        Connection connection=null;
        //String url="jdbc:mysql://localhost:3306/user";
        try {
           // driver = new Driver();
           // DriverManager.registerDriver(driver);
            Class.forName(driver);
           connection= DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return connection;
    }
    public static void release(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
        if(connection!=null)
        {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(preparedStatement!=null)
        {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(resultSet!=null)
        {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}

而登录的jdbc代码:

public void login1(String account,String password) throws SQLException {
        Connection connection = JDBCUtil.getConnection();
        String sql="select  * from  user where account=? and password=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,account);
        preparedStatement.setString(2,password);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next())
        {
            String name=resultSet.getString("account");
            System.out.println(name+"登录成功!");
        }
        else
        {
            System.out.println("登录失败!");
        }
       JDBCUtil.release(connection, preparedStatement, resultSet);

    }

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值