jdbc封装代码

Posted 栖息地

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdbc封装代码相关的知识,希望对你有一定的参考价值。


jdbc封装代码


package jdbcUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import jdbc.RowMap;

public class JdbcUtil {
    public static Connection getConnection(){
        Connection connection=null;
        //加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //2创建连接(主机名,端口号,用户名,密码)
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "123456");
        }
         catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         return connection;
    
    }

    public static void close(Connection connection){
        try {
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static int executeUpdate(String sql,Object... params){
        int result=0;
        Connection connection=getConnection();
        PreparedStatement pstmt;
        try {
            pstmt = connection.prepareStatement(sql);
            if(params!=null){
                for(int i=0;i<params.length;i++){
                    pstmt.setObject(i+1, params[i]);
                }
            result=pstmt.executeUpdate();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            close(connection);
        }
        return result;
    }   
    
    
    public static <T>List<T> executeSelect(String sql,RowMap<T> rowMap,Object...params){
        List<T> result=new ArrayList<>();
        Connection connection=getConnection();
        try {
            PreparedStatement pstmt=connection.prepareStatement(sql);
            if(params!=null){
                for(int i=0;i<params.length;i++){
                    pstmt.setObject(i+1, params[i]);
                }
            }
            ResultSet rs=pstmt.executeQuery();
            while(rs.next()){
                //将数据行 映射到对象中
                T t=rowMap.rowMapping(rs);
                result.add(t);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            close(connection);
        }
        return result;
    } 

}


JdbcUtil.java


package jdbcUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import jdbc.RowMap;

public class JdbcUtil {
    public static Connection getConnection(){
        Connection connection=null;
        //加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //2创建连接(主机名,端口号,用户名,密码)
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "123456");
        }
         catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         return connection;
    
    }

    public static void close(Connection connection){
        try {
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static int executeUpdate(String sql,Object... params){
        int result=0;
        Connection connection=getConnection();
        PreparedStatement pstmt;
        try {
            pstmt = connection.prepareStatement(sql);
            if(params!=null){
                for(int i=0;i<params.length;i++){
                    pstmt.setObject(i+1, params[i]);
                }
            result=pstmt.executeUpdate();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            close(connection);
        }
        return result;
    }   
    
    
    public static <T>List<T> executeSelect(String sql,RowMap<T> rowMap,Object...params){
        List<T> result=new ArrayList<>();
        Connection connection=getConnection();
        try {
            PreparedStatement pstmt=connection.prepareStatement(sql);
            if(params!=null){
                for(int i=0;i<params.length;i++){
                    pstmt.setObject(i+1, params[i]);
                }
            }
            ResultSet rs=pstmt.executeQuery();
            while(rs.next()){
                //将数据行 映射到对象中
                T t=rowMap.rowMapping(rs);
                result.add(t);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            close(connection);
        }
        return result;
    }
    
    

}


jdbcBean.java


package jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;import jdbcUtil.JdbcUtil;class jdbcBean {
    public static void main(String[] args) {
        List<Student> list=select();
        System.out.println(list);
        /*jdbcBean jdbcBean=new jdbcBean();
        jdbcBean.tset();*/
        /*delete();*/
        /*add();*/
    }
    
    public static int delete(){ 
        return JdbcUtil.executeUpdate("delete from student where sid=? and sname=?", 10,"1tê???");
        
    }
    
    public static int update(){ 
        return JdbcUtil.executeUpdate("update student set sname=?,age=?,sex=? where sid=?","mojie",2,"D?",1);
        
    }
    
    public static int add(){
        return JdbcUtil.executeUpdate("insert into student(sname,age,sex) values(?,?,?)", "í?1t",3,"D?");
        
    }
     public static List select(){
         
         return jdbcUtil.JdbcUtil.executeSelect("select * from student",new RowMap<Student>(){
             @Override
             public Student rowMapping(ResultSet rs){
                 Student student=new Student(); 
                try {
                    student.setAge(rs.getInt("age"));
                    student.setName(rs.getString("sname"));
                    student.setSex(rs.getString("sex"));
                    student.setSid(rs.getInt("sid"));
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 return student;
             }
         }, null);
            /*Connection connection=JdbcUtil.getConnection();
            List<Student> list=new ArrayList<>();
            try {
                PreparedStatement pstmt=connection.prepareStatement("select * from student");
                ResultSet rs= pstmt.executeQuery();
                while (rs.next()) {
                    Student student=new Student();  
                    student.setAge(rs.getInt("age"));
                    student.setName(rs.getString("sname"));
                    student.setSex(rs.getString("sex"));
                    student.setSid(rs.getInt("sid"));
                    list.add(student);
                    
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                JdbcUtil.close(connection);
            }
            return list;*/
    }
     public void tset(){
         HashMap<String,Student> sHashMap=new HashMap<>();
         for(int i=0;i<4;i++){
             System.out.println("qingshuru");
             Scanner scanner=new Scanner(System.in);
             Integer sid=scanner.nextInt();
             String sname=scanner.next();
             Integer age=scanner.nextInt();
             String sex=scanner.next();
             Student student=new Student(sname, age, sid, sex);
             sHashMap.put("Student"+i, student);
         }
         for(int i=0;i<4;i++){
             System.out.println(sHashMap.get("Student"+i));
         }
             
        
     }
}


RowMap.java


package jdbc;

import java.sql.ResultSet;

public interface RowMap<T> {
    public T rowMapping(ResultSet rs);
}


Student.java


package jdbc;

public class Student {
    private String name;
    private int age;
    private int sid;
    private String sex;
    public String getName() {
        return name;
    }
    public Student(String name, int age, int sid, String sex) {
        super();
        this.name = name;
        this.age = age;
        this.sid = sid;
        this.sex = sex;
    }
    public int getSid() {
        return sid;
    }
    public void setSid(int sid) {
        this.sid = sid;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
    }
    public Student(String name, int age, String sex) {
        super();
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    
}





以上是关于jdbc封装代码的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装

jdbc封装代码

关于mysql驱动版本报错解决,Cause: com.mysql.jdbc.exceptions.jdbc4Unknown system variable ‘query_cache_size(代码片段

回归 | js实用代码片段的封装与总结(持续更新中...)

VsCode 代码片段-提升研发效率