Java JDBC封装模式

模仿DBUtils里面的一些用法,下面是一些简单的实现数据集的操作的方法


下面使用到的两个bean。首先是userbean

package bean;

public class user {
    
    String username;
    String password;
    
    public user(){
        username=null;
        password=null;
    }
    
    @Override
    public String toString() {
        return "user [username=" + username + ", password=" + password + "]";
    }
    public user(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

}

接下来是itemsbean的代码,都很简单

package bean;

public class Items {

    String title;
    String contents;
    public Items(){
        title=null;
        contents=null;
    }
    public Items(String title, String contents) {
        super();
        this.title = title;
        this.contents = contents;
    }
    @Override
    public String toString() {
        return "Items [title=" + title + ", contents=" + contents + "]";
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContents() {
        return contents;
    }
    public void setContents(String contents) {
        this.contents = contents;
    }
    
}


接下来就是重头戏了,也即是我们的DBHelper类,主要的思想是其中对bean的操作,还有是如何将数据集进行封装的

package DBUtils;

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 bean.Items;
import bean.user;


/**
 * 易错点:
 * 需要注意的是items表中有一项在数据库中为content,而bean里设置出错,设置成了contents,尤其应该注意;
 * @author Summer
 *
 */
public class Dbutiler {
    
    static String DRIVER="com.mysql.jdbc.Driver";
    static String CONNECTIONURL="jdbc:mysql://127.0.0.1:3306/summer";
    static String ROOT="root";
    static String PASSWORD="mysql";
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet result=null;
    
    public Dbutiler(){
        
        try {
            Class.forName(DRIVER);
            conn=(Connection) DriverManager.getConnection(CONNECTIONURL, ROOT, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        if(conn!=null){
            System.out.println("Connected!");
        }
    }
    
    
    //下面是获得用户信息数据集
    public List getUserInfo(String sql){
        user tempUser=null;
        List userList=new ArrayList();
        try {
            ps=conn.prepareStatement(sql);
            result=ps.executeQuery();
            while(result.next()){
                tempUser=new user();
                String userName=result.getString("username");
                String passWord=result.getString("password");
                tempUser.setUsername(userName);
                tempUser.setPassword(passWord);
                userList.add(tempUser);
                
                //感悟就是,每次invoke之后,都要及时的进行置空,否则可能得不到新值
                tempUser=null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                ps=null;
            }
            if(result!=null){
                try {
                    result.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                result=null;
            }
        }
        return userList;
        
    }

    //下面是插入用户的方法
    public boolean addUser(user userInfo){
        int flag=0;
        try {
            String sql="insert into user(username,password) values(?,?);";
            ps=(PreparedStatement) conn.prepareStatement(sql);
            ps.setString(1,userInfo.getUsername());
            ps.setString(2, userInfo.getPassword());
            flag=ps.executeUpdate();
            
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                ps=null;
            }
            if(result!=null){
                try {
                    result.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                result=null;
            }
        }
        if(flag!=0){
            return true;
        }else{
            return false;
        }
    }
    
    
    public List getItemsInfo(String sql){
        Items tempItem=null;
        List ItemsInfo=new ArrayList();
        try {
            ps=conn.prepareStatement(sql);
            result=ps.executeQuery();
            while(result.next()){
                tempItem=new Items();
                String Title=result.getString("title");
                String Contents=result.getString("content");
                tempItem.setTitle(Title);
                tempItem.setContents(Contents);
                ItemsInfo.add(tempItem);
                
                //感悟就是,每次invoke之后,都要及时的进行置空,否则可能得不到新值
                tempItem=null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                ps=null;
            }
            if(result!=null){
                try {
                    result.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                result=null;
            }
        }
        return ItemsInfo;
        
    }
    
    //下面是添加数据信息的方法
        public boolean addOneItem(Items item){
            int flag=0;
            try {
                String sql="insert into items(title,content) values(?,?);";
                ps=(PreparedStatement) conn.prepareStatement(sql);
                ps.setString(1,item.getTitle());
                ps.setString(2, item.getContents());
                flag=ps.executeUpdate();
                
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                if(ps!=null){
                    try {
                        ps.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    ps=null;
                }
                if(result!=null){
                    try {
                        result.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    result=null;
                }
            }
            if(flag!=0){
                return true;
            }else{
                return false;
            }
        }
    
    
    public static void main(String []args){
        Dbutiler helper=new Dbutiler();
        //对存储的用户信息进行测试
        user one=new user("Rui","Tiger");
        helper.addUser(one);
        List res=new ArrayList();
        res=helper.getUserInfo("select * from user;");
        for(int i=0;iitemsList=new ArrayList();
        itemsList=helper.getItemsInfo("select * from items;");
        if(itemsList.isEmpty()){
            System.out.println("This resultSet is empty!");
        }else{
            for(int i=0;i

相应的我们运行完程序之后,是可以在MySQL的数据库下进行查看的,确实是插入进去了,而查找的时候也会返回我们想要查找的并且符合要求的值。

你可能感兴趣的:(Java JDBC封装模式)