Jdbc写一个访问数据库的工具类

操作的工具类


package com.zjw.jdbc2;
/**
 * jdbc操作的工具类
 * @author Administrator
 *
 */

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

public class JdbcUtils
{
    private JdbcUtils() {}

    private static String url = "jdbc:mysql://localhost:3306/aaa?useSSL=false&serverTimezone=UTC";
    private static String user = "root" ;
    private static String password = "123456";

    public static Connection getConnection() 
    {
        Connection con = null;
        try
        {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接
            con = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException | SQLException e)
        {
            e.printStackTrace();
        }
        //返回连接结果
        return con;
    }
    /**
     *  关闭连接 包含三个参数
     * @param con 连接对象
     * @param sta Statement对象
     * @param res 结果集对象
     */
    public static void closeConnection(Connection con,Statement sta,ResultSet res) 
    {
        try
        {
            if(res != null)
            res.close();
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        try
        {
            if(sta != null)
            sta.close();
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        try
        {
            if(con != null)
            con.close();
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
    /**
     * 关闭连接 包含两个参数
     * @param con 连接对象
     * @param sta Statement对象
     */
    public static void closeConnection(Connection con,Statement sta) 
    {
        try
        {
            if(sta != null)
            sta.close();
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        try
        {
            if(con != null)
            con.close();
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
}

利用工具类来操作数据库

package com.zjw.jdbc2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 * 使用jdbc编写的工具类操作数据库
 * @author Administrator
 *
 */
public class JdbcUtilTest
{
    public static void main(String[] args)
    {
        //获取连接
        Connection con = JdbcUtils.getConnection();
        //操作数据库
        String sql = "SELECT * FROM category;";
        try
        {
            PreparedStatement pst = con.prepareStatement(sql);
            ResultSet res = pst.executeQuery();
            while(res.next())
            {
                String sid = res.getString("sid");
                String sname = res.getString("sname");
                System.out.println(sid+ " "+ sname);
            }
            //关闭连接
            JdbcUtils.closeConnection(con, pst, res);
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
}

results matching ""

    No results matching ""