1.目录结构:
->con
—> DbHandle: 数据库操作把手
—> PropertiesUtil: 读取properties的操作
->config
---->config.properties:属性配置
->model
---->Admin:就是普通的model
2.properties配置属性:
# mysql config v5.7
db_connect = com.mysql.jdbc.Driver
db_url = localhost
db_port = 3306
db_name = db_java
db_username = root
db_password = 123456
3.PropertiesUtil.java:
package com;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
public class PropertiesUtil {
private static Properties _prop = new Properties();
public static void readProperties(){
try {
InputStream in = PropertiesUtil.class.getResourceAsStream("/config/config.properties");
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
_prop.load(bf);
}catch (IOException e){
e.printStackTrace();
}
}
public static String getProperty(String key){
return _prop.getProperty(key);
}
}
4.Admin.java :
package model;
public class Admin {
private String username;
private String password;
private String email;
private String tel;
private String time;
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTel() {
return tel;
}
@Override
public String toString() {
return "{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", tel='" + tel + '\'' +
", time='" + time + '\'' +
'}';
}
public void setTel(String tel) {
this.tel = tel;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
5.DbHandle源码展示:
package com;
import model.Admin;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class DbHandle {
private String dbConnect;
private String dbUrl;
private String dbPort;
private String dbName;
private String dbUsername;
private String dbPassword;
private static Connection conn; // 数据库连接对象
private static Statement stmt; // 数据库操作把柄对象
public DbHandle() { // 加载配置文件
PropertiesUtil.readProperties();
this.dbConnect = PropertiesUtil.getProperty("db_connect");
this.dbUrl = PropertiesUtil.getProperty("db_url");
this.dbPort = PropertiesUtil.getProperty("db_port");
this.dbName = PropertiesUtil.getProperty("db_name");
this.dbUsername = PropertiesUtil.getProperty("db_username");
this.dbPassword = PropertiesUtil.getProperty("db_password");
}
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());
// }
}
6.关于:
转载请声明出处!