📄 connectbean.java~5~
字号:
package work.tools;
import java.util.Properties;
import java.sql.*;
import java.io.*;
public class ConnectBean implements Serializable{
private Connection conn = null;
private ResultSet result = null;
private PreparedStatement ps = null;
private String driver;
private String url;
private String user;
private String password;
public ConnectBean() {
}
public Connection getConnection() {
return conn;
}
public void setConnection(Connection conn) {
this.conn = conn;
}
public boolean initPropery() {
try {
// Properties ps = new Properties();
// ps.load(getClass().getResourceAsStream("/conf/db.properties"));
// this.driver = ps.getProperty("driver");
// this.url = ps.getProperty("url");
// this.user = ps.getProperty("user");
// this.password = ps.getProperty("password");
this.driver = "com.mysql.jdbc.Driver";
this.url = "jdbc:mysql://localhost/homework?useUnicode=true&characterEncoding=gb2312";
this.user = "root";
this.password = "123456";
System.out.println(driver);
System.out.println(url);
System.out.println(user);
System.out.println(password);
return true;
} catch (Exception e) {
System.out.println("initProperty"+e);
return false;
}
}
public void setAutoCommit(boolean bl){
try {
conn.setAutoCommit(bl);
} catch (SQLException ex) {
}
}
public void commit(){
try {
conn.commit();
} catch (SQLException ex) {
}
}
public boolean openConnection() {
if (!this.initPropery())
return false;
try {
Class.forName(this.driver);
} catch (ClassNotFoundException e) {
System.out.println(e);
return false;
}
try {
if (conn == null)
this.conn = DriverManager.getConnection(this.url, this.user,
this.password);
} catch (SQLException e2) {
System.out.println("oppen connection error"+e2);
return false;
}
return true;
}
public PreparedStatement createPreparedStatement(String sqlString,
boolean isScrollAndUpdateTable) throws SQLException {
if (isScrollAndUpdateTable)
ps = conn.prepareStatement(sqlString,
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
else
ps = conn.prepareStatement(sqlString);
return ps;
}
public PreparedStatement createPreparedStatement(String sqlString) throws
SQLException {
ps = conn.prepareStatement(sqlString);
return ps;
}
public int executeUpdate() throws SQLException {
return ps.executeUpdate();
}
public boolean next() throws SQLException {
return result != null ? result.next() : false;
}
public ResultSet executeQuery() throws SQLException {
this.result = ps.executeQuery();
return result;
}
public String getData(int index) throws SQLException {
return result.getString(index);
}
public int getIntData(int index) throws SQLException {
return result.getInt(index);
}
public float getFltData(int index) throws SQLException {
return result.getFloat(index);
}
public void setData(int index, String data) throws SQLException {
ps.setString(index, data);
}
public void setIntData(int index, int data) throws SQLException {
ps.setInt(index, data);
}
public void setFltData(int index, float data) throws SQLException {
ps.setFloat(index, data);
}
public void resetResult() throws SQLException {
this.result = null;
}
public String iso2gb(String qs) {
try {
if (qs == null)
return "NULL";
else {
// Used for TurboLinux
return new String(qs.getBytes("iso-8859-1"), "gb2312");
}
} catch (Exception e) {
System.err.println("iso2gb error:" + e.getMessage());
}
return "NULL";
}
public String gb2iso(String qs) {
try {
if (qs == null)
return "NULL";
else {
//Used for TurboLinux
return new String(qs.getBytes("gb2312"), "iso-8859-1");
}
} catch (Exception e) {
System.err.println("gb2iso error:" + e.getMessage());
}
return "NULL";
}
public void close() throws SQLException {
System.out.println("conn Close()");
this.conn.close();
}
public void finalize() throws Throwable {
System.out.println("conn Close()...");
}
public static void main(String s[]) throws SQLException {
System.out.println("Hello");
ConnectBean cb = new ConnectBean();
if (!cb.openConnection()) {
System.out.println("连接数据失败");
System.exit(1);
return;
}
cb.createPreparedStatement("Select * From userinfo", false);
ResultSet rs = cb.executeQuery();
while (rs.next()) {
System.out.println(rs.getString(2));
}
cb.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -