码迷,mamicode.com
首页 > 数据库 > 详细

jdbc封装代码

时间:2019-01-04 18:45:04      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:state   name   ...   bean   exti   pst   res   ram   setname   


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封装代码

标签:state   name   ...   bean   exti   pst   res   ram   setname   

原文地址:https://www.cnblogs.com/qixidi/p/10221719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!