java数据库操作类
2019-01-17 · 1 阅读
package mengyiju;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class db {
public Connection conn;
private String table;
private String prefix;
private String where="";
private String order="";
private String limit="";
public db(String table)
{
try {
Properties prop = db.config();
this.conn=db.getConn();
this.table=this.checkcase(table);
this.prefix=prop.get("prefix").toString();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
//构造类
public static db M(String table)
{
return new db(table);
}
//添加表前双缀
public String checkcase(String str){
char[] head=str.toCharArray();
char h=head[0];
if(!Character.isLowerCase(h)){
return this.prefix+str.toLowerCase();
}else{
return str;
}
}
public db where(String where)
{
if(this.where=="")
{
this.where=" where "+where;
}else {
this.where=this.where+" and ("+where+")";
}
return this;
}
public db limit(int a)
{
this.limit=" limit 0,"+String.valueOf(a);
return this;
}
public db limit(int a,int b)
{
this.limit=" limit "+String.valueOf(a)+","+String.valueOf(b);
return this;
}
public db order(String order)
{
this.order=" ORDER BY "+order;
return this;
}
//查打一个
public ResultSet select() throws Exception
{
Statement stmt = null;
stmt = this.conn.createStatement();
String sql="SELECT * FROM "+this.table+" "+this.where+" "+this.order+" "+this.limit;
ResultSet rs = stmt.executeQuery(sql);
this.where="";
this.order="";
this.limit="";
return rs;
}
//读取配置
public static Properties config() throws IOException
{
String root = Thread.currentThread().getContextClassLoader().getResource("../").toString();
String path =root.substring(root.indexOf("file")+"file".length()+2);
FileInputStream fis = new FileInputStream(path+"db.properties");
Properties prop = new Properties();
prop.load(fis);
fis.close();
return prop;
}
// JDBC 驱动名及数据库 URL
public static Connection getConn() throws IOException {
Connection conn = null;
try {
Properties prop = db.config();
Class.forName(prop.get("jdbc.driver_class").toString());
conn = DriverManager.getConnection(prop.get("jdbc.connection.url").toString()+"?user="+prop.get("jdbc.connection.username").toString()+"&password="+prop.get("jdbc.connection.password").toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
使用方法如下
try {
ResultSet abc=db.M("admin_log").order("admin_log_id desc").limit(2,3).select();
while(abc.next()){
int id = abc.getInt("admin_log_id");
String name = abc.getString("ip");
String url = abc.getString("is_correct");
// 输出数据
out.print("ID: " + id);
out.print(", ip: " + name);
out.print(", correct: " + url);
out.print("\n");
}
} catch (Exception e1) {
// TODO 自动生成的 catch 块
out.print(e1.getLocalizedMessage());
}
配置文件在/WEB-INF/db.properties
jdbc.driver_class=com.mysql.jdbc.Driver
jdbc.connection.username=帐号
jdbc.connection.url=jdbc:mysql://localhost/数据库名
jdbc.connection.password=密码
prefix=表前缀