📄 dao.java
字号:
package com.etagmedia.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.sql.*;
public class Dao {
private static Log log = LogFactory.getLog(Dao.class);
String sDBDriver = "org.logicalcobwebs.proxool.ProxoolDriver";
// String sConnStr = "jdbc:mysql://localhost:3306/icmp_db?useUnicode=true&characterEncoding=UTF-8";
String sConnStr = "proxool.example:com.mysql.jdbc.Driver:jdbc:mysql://localhost:3306/readbook?useUnicode=true&characterEncoding=UTF-8";
Connection conn;
ResultSet rs;
Statement stmt;
PreparedStatement pstmt;
String user = "root";
String password = "";
public Dao() {
//sDBDriver = "com.mysql.jdbc.Driver";
rs = null;
stmt = null;
try {
Class.forName(sDBDriver);
} catch (ClassNotFoundException classnotfoundexception) {
log.error("dao(): " + classnotfoundexception.getMessage());
}
}
public ResultSet executeQuery(String s) {
try {
conn = DriverManager.getConnection(sConnStr, user, password);
stmt = conn.createStatement();
rs = stmt.executeQuery(s);
} catch (SQLException sqlexception) {
log.error("db.executeQuery: " + sqlexception.getMessage());
log.error("db.executeQuerystrSQL: " + s);
}
return rs;
}
public int executeUpdate(String s) {
int rows = 0;
try {
conn = DriverManager.getConnection(sConnStr, user, password);
stmt = conn.createStatement();
rows = stmt.executeUpdate(s);
stmt.close();
conn.close();
} catch (SQLException sqlexception) {
log.error("db.executeUpdate: " + sqlexception.getMessage());
log.error("db.executeUpadatestrSQL: " + s);
}
return rows;
}
public ResultSet executeQueryPs(String ps, Object[] args) {
try {
conn = DriverManager.getConnection(sConnStr, user, password);
pstmt = conn.prepareStatement(ps);
if (args != null) {
for (int i = 0; i < args.length; i++) {
pstmt.setObject(i + 1, args[i]);
}
}
rs = pstmt.executeQuery();
} catch (SQLException sqlexception) {
log.error("db.executeQueryPs: " + sqlexception.getMessage());
log.error("db.executeQuerystrSQL: " + ps);
}
return rs;
}
public int executeUpdatePs(String ps, Object[] args) {
int rows = 0;
try {
conn = DriverManager.getConnection(sConnStr, user, password);
pstmt = conn.prepareStatement(ps);
if (args != null) {
for (int i = 0; i < args.length; i++) {
pstmt.setObject(i + 1, args[i]);
}
}
pstmt.executeUpdate();
} catch (SQLException sqlexception) {
log.error("db.executeQueryPs: " + sqlexception.getMessage());
log.error("db.executeQuerystrSQL: " + ps);
}
return rows;
}
public void close() throws SQLException {
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -