java使用jdbc连接数据库

1、获得(设置)连接sql的参数

mysql连接需要的参数有驱动driver、数据库链接url、用户user、登录密码password
这些数据可以直接放在java类中,但是一般为安全我们放在配置文件properties中,通过读取配置文件参数来获取

 /**
     * 四个基本的mysql配置信息
     */
    private static String driver;
    private static String url;
    private static String userName;
    private static String password;
    //静态代码块来初始化sql信息
    static {
        //创建获取配置文件对象
        Properties properties = new Properties();
        //得到一个读取配置文件流对象
        InputStream in = DBUtil.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //读取文件获得数据
        userName = properties.getProperty("userName");
        password = properties.getProperty("password");
        url = properties.getProperty("url");
        driver = properties.getProperty(driver);
        try {
            //加载驱动类
            Class.forName("driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

2、加载驱动

 try {
            //加载驱动类
            Class.forName("driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

3、Connection对象连接mysql

连接mysql需要使用Connection对象来实现,其中Connect对象需要DriverManager对象来获得

private static Connection con;
    public static Connection getConnection(){
        //数据库连接
        try{
        con = DriverManager.getConnection("url","userName","password");
        }catch (Exception e){
            e.printStackTrace();
        }
        return con;
    }

4、获得一个PrepareStatement对象来操作数据库

//sql例子
String sql = "select * from user where user name like ?";//模糊查询
//其中?为一个占位符,最后需要对其赋值
PrepareStatement pstm = connection.prepareStatement(sql);
//同时包含了一个预编译过程
pstm.setObject(1,"%林%");//1标识占位符的位置
//执行sql语句,使用ResultSet对象来存储我们查询的结果。
ResultSet rs = pstm.executeQuery();//只是执行查操作

5、资源释放:

sql执行完后,为了避免资源浪费,我们需要关闭Connection、PrepareSatement、ResultSet对象。**重要:**关闭的顺序要遵循“先开后关的原则”;也就是关闭顺序为ResultSet -》 PrepareSatement -》 Connection;代码实例为如下:

public static boolean closeResource(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
        boolean flag = true;
        if (resultSet != null) {
            try {
                resultSet.close();
                //GC回收
                resultSet = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }

        if (preparedStatement != null) {
            try {
                preparedStatement.close();
                //GC回收
                preparedStatement = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }

        if (connection != null) {
            try {
                connection.close();
                //GC回收
                connection = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }
        return flag;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值