源码:
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());
// }
}