java简单封装mysql操作 ------ java开发日常

   日期:2020-05-30     浏览:115    评论:0    
核心提示:源码:package com;import model.Admin;import java.sql.*;import java.util.ArrayList;import java.util.Date;import java.util.List;public class DbTool { private String dbConnect = com.mysql.jdbc.Driver; private String dbUrl = localhost; pjava
源码:
package com;

import model.Admin;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DbTool {
    private String dbConnect = "com.mysql.jdbc.Driver";
    private String dbUrl = "localhost";
    private String dbPort = "3306";
    private String dbName = "db_java";
    private String dbUsername = "root";
    private String dbPassword = "123456";

    private static Connection conn;  // 数据库连接对象
    private static Statement stmt;   // 数据库操作把柄对象

    
    public Connection connect(){
        String DB_URL = "jdbc:mysql://"+this.dbUrl+":"+this.dbPort+"/"+this.dbName;
        try{
            // 注册 JDBC 驱动
            Class.forName(this.dbConnect);
            // 打开链接
            conn = DriverManager.getConnection(DB_URL, dbUsername, dbPassword);
            stmt = conn.createStatement();
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
        return conn;
    }

    
    public List<Admin> selectAll(){
        Connection con = this.connect();
        String sql = "select * from `admin`";
        List<Admin> list = new ArrayList<Admin>();
        try{
            stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()){
                Admin admin = new Admin();
                admin.setUsername(rs.getString("username"));
                admin.setPassword(rs.getString("password"));
                admin.setEmail(rs.getString("email"));
                admin.setTel(rs.getString("tel"));
                admin.setTime(rs.getTimestamp("time").toString());

                list.add(admin);
            }
        }catch (SQLException e){
        } catch (NullPointerException e){
        }finally {
            try {con.close();}catch (SQLException e){ }
            try {stmt.close();}catch (SQLException e){ }
        }
        return list;
    }

    
    public Boolean add(Admin user){
        String sql = "INSERT INTO `admin`\n" +
                     "(username, `password`, email, tel, time)\n" +
                     "VALUES\n" +
                     "(?,?,?,?,?)";
        Connection con = this.connect();
        PreparedStatement pstmt = null;

        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, user.getUsername());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getEmail());
            pstmt.setString(4, user.getTel());
            pstmt.setTimestamp(5, new Timestamp(new Date().getTime()));
            int bool = pstmt.executeUpdate();
            if(bool == 1){
                return true;
            }else{
                return false;
            }
        }catch(SQLException e) {
            e.printStackTrace();
            return false;
        }finally {
            try {con.close();}catch (SQLException e){ }
            try { pstmt.close(); }catch (SQLException e){ }
        }
    }

    
    public Admin selectOfkeyAndValue(String key, String value){
        Admin admin = null;
        Connection con = this.connect();
        String sql = "select * from `admin` where "+key+" = '"+value+"'";
        try{
            stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()){
                admin = new Admin();
                admin.setUsername(rs.getString("username"));
                admin.setPassword(rs.getString("password"));
                admin.setEmail(rs.getString("email"));
                admin.setTel(rs.getString("tel"));
                admin.setTime(rs.getTimestamp("time").toString());
            }
            return admin;
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {con.close();}catch (SQLException e){ }
            try {stmt.close();}catch (SQLException e){ }
        }
        return null;
    }


// public static void main(String[] args) {
// DbHandle db = new DbHandle();
// Admin t1 = db.selectOfkeyAndValue("username", "admin");
// System.out.println(t1.toString());
// }
}

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
更多>相关资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服