jdbc书写模板(复制粘贴)

本文详细介绍了Java中JDBC的使用,从基础的普通查询和插入,到安全插入和DBUtil对象封装,再到线程池的实现,特别是c3p0数据库连接池的配置。随后探讨了Spring如何与JDBC进行整合,通过不同版本的实践,展示了从XML配置到注解简化数据库操作的过程,并涉及到事务控制的注解使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、普通版

1.1、普通查询

public class MyTest {
   	
    public static void main(String[] args) {
   
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
   
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");

           //2.获取连接
conn=
         DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456"); 
            //3.获取数据库操作对象
            String sql = "select * from user_table";
            ps = conn.prepareStatement(sql);
            //4.执行sql
            rs = ps.executeQuery();
            //5.处理结果集
            while(rs.next()){
   
                String a = rs.getString(1);
                String b = rs.getString(2);
                String c = rs.getString(3);
                System.out.println(a+","+b+","+c);
            }
        }catch(Exception e){
   
            e.printStackTrace();
        }finally {
   
            //6.关闭资源
--------------------------------------------------------------------
try {
   
    if (conn != null) {
   
        conn.close();
    }
} catch (Exception e) {
   
    e.printStackTrace();
}

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

1.2、普通插入

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Exer2 {
   
    public static void main(String[] args) {
   
        Connection conn = null;
        PreparedStatement ps = null;
        try{
   
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1","root","root");
            //3.获取数据库操作对象
            String sql = "insert into ccc values(9,'a',9)";
            ps = conn.prepareStatement(sql);
            //4.执行sql
            int count = ps.executeUpdate();
        }catch (Exception e){
   
            e.printStackTrace();
        }finally {
   
            try {
   
                if(conn != null){
   
                    conn.close();
                }
            }catch (Exception e){
   
                e.printStackTrace();
            }
            try {
   
                if(ps != null){
   
                    ps.close();
                }
            }catch (Exception e){
   
                e.printStackTrace();
            }
        }
    }
}

2、安全插入

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Exer3 {
   
    public static void main(String[] args) {
   
        Connection conn = null;
        PreparedStatement ps = null;
        try{
   
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1","root","root");
            //3.获取数据库操作对象
            String sql = "insert into ccc values(?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1,"8");
            ps.setString(2,"v");
            ps.setString(3,"8");
            //4.执行sql
            int count = ps.executeUpdate();
        }catch (Exception e){
   
            e.printStackTrace();
        }finally {
   
            try {
   
                if(conn != null){
   
                    conn.close();
                }
            }catch (Exception e){
   
                e.printStackTrace();
            }
            try {
   
                if(ps != null){
   
                    ps.close();
                }
            }catch (Exception e){
   
                e.printStackTrace();
            }
        }
    }
}

3、将常用对象封装到DBUtil中

DBUtil类

public class DBUtil {
   
    private DBUtil(){
   }
    static {
   
        try {
   
            Class.forName("con.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
   
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {
   

        return DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");

    }

    public static void close(Connection connection, Statement ps,ResultSet rs){
   
        try{
   
            if(ps!=null){
   
                ps.close();
            }
        }catch(Exception e){
   
            e.printStackTrace();
        }
        try{
   
            if(connection!=null){
   
                connection.close();
            }
        }catch (Exception e){
   
            e.printStackTrace();
        }
        try{
   
            if(rs!=null){
   
                rs.close();
            }
        }catch (Exception e){
   
            e.printStackTrace();
        }
    }
}

实现类

public class Thried {
   
    public static void main(String[] args) {
   
        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        try {
   
            //1.加载驱动
            //2.获取连接
            conn = DBUtil.getConnection();
            //3.获取数据库操作对象
            String sql = "select * from user_table";
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值