欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

数据库连接JDBC工具包

程序员文章站 2024-03-20 22:19:40
...

JDBC连接的基本步骤:

  1. 加载JDBC驱动管理{Class.forName(com.mysql.jdbc.Driver0)//加载类,名字为myql包下的jdbc的驱动类,为固定写法}
  2. 连接数据库
  3. 开启Statement,执行sql语句
  4. 得到结果集 (仅查找)
  5. 对结果集进行操作
  6. 关闭释放资源(栈式关闭,先关闭结果集,再关闭statement,最后关闭连接)

JDBC工具包

package com.company.jdbc;

import java.sql.*;

public class JDBCUtil {

    private static final String url = "dbc:mysql://localhost:3306/test";
    private static final String username = "root";
    private static final String password = "123456";
    private static JDBCUtil instance = null;

    //定义一个私有构造函数
    private JDBCUtil(){}

    //加载驱动
    static {
        try{
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //获取唯一实例
    public static JDBCUtil getInstance(){
        if(instance == null) {
            synchronized (JDBCUtil.class) {
                if(instance == null) {
                    instance = new JDBCUtil();
                }
            }
        }
        return instance;
    }
    //获取Connection对象的方法
    public Connection getConnection(){
        Connection con = null;

        try {
            con = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
    //释放资源
    public void free(Connection connection, Statement stmt, ResultSet rs) {

        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

查询

package com.company.jdbc;

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

public class SelectTest {

    private static PreparedStatement ps = null;
    private static ResultSet rs = null;

    public static void main(String[] args) {
        Connection connection = JDBCUtil.getConnection();
        String sql = "String sql=\"select * from user \";";
        try{
            ps = connection.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                System.out.print(rs.getInt(1)+"--"+rs.getString("username"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.free(connection, ps, rs);
        }
    }
}

修改

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

public class UpdateTest {
    private static PreparedStatement ps = null;

    public static void main(String[] args) {
        Connection connection = JDBCUtil.getConnection();
        String sql="update user set username =? where id = ?";
        try{
            ps = connection.prepareStatement(sql);
            ps.setObject(1, "小暖宝");
            ps.setObject(2, 1);
            int result = ps.executeUpdate();
            System.out.println(result);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.free(connection, ps, null);
        }
    }
}

删除和添加同理