jdbc封装代码

java

浏览数:507

2019-1-7

片段 1片段 2片段 3片段 4片段 5


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,"¹þÊ¿Ææ");
        
    }
    
    public static int update(){ 
        return JdbcUtil.executeUpdate("update student set sname=?,age=?,sex=? where sid=?","mojie",2,"ÐÛ",1);
        
    }
    
    public static int add(){
        return JdbcUtil.executeUpdate("insert into student(sname,age,sex) values(?,?,?)", "ÍÛ¹þ",3,"ÐÛ");
        
    }
     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
    }
    
    
}