📄 dbconnect.java
字号:
package cn.dxm.dbconn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import cn.dxm.XMLAnaysis.Dom.DomParserForPathSet;
public class DBConnect {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
private static String url = null;
private static String className = null;
private static String username = null;
private static String password = null;
public Connection getConnection() {
// String className = null;
// String url = null;
// String username = null;
// String password = null;
//
// Connection conn = null;
//
// if (className != null && url != null && username != null&& password
// != null) {
// className = this.className;
// url = this.url;
// username = this.username;
// password = this.password;
//
// } else {
DomParserForPathSet dom = new DomParserForPathSet();
Map map = dom.ParserXML("src/ParameterSet.xml");
this.className = (String) map.get("className");
this.url = (String) map.get("url");
this.username = (String) map.get("user");
this.password = (String) map.get("password");
// }
try {
if (className != null)
Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(this.url, this.username,
this.password);
} catch (SQLException e) {
e.printStackTrace();
}
this.conn = conn;
return conn;
}
public ResultSet executeQuery(String sql) {
ResultSet rs = null;
Statement stmt = null;
Connection conn = null;
if (this.conn == null) {
conn = this.getConnection();
} else {
conn = this.conn;
}
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("the execute sql sentence is:" + sql);
}
return rs;
}
public boolean executeUpdate(String sql) {
int result = 0;
Statement stmt = null;
Connection conn = null;
if (this.conn == null) {
conn = this.getConnection();
} else {
conn = this.conn;
}
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
try {
result = stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("the exeucte sentence is:" + sql);
}
return result > 0;
}
public void CloseDB(){
try {
conn.close();
} catch (SQLException SqlE) {
SqlE.printStackTrace();
//out.print("数据库关闭失败!");
}
}
// public static void main(String args[]){
// String updateSql="delete from imageinfo where id<3";
// String querySql="select * from imageinfo";
// DBConnect dbconn=new DBConnect();
//
// dbconn.executeUpdate(updateSql);
//
// ResultSet rs=dbconn.executeQuery(querySql);
// try{
//
// while(rs.next()){
// System.out.println(rs.getString("name"));
// System.out.println(rs.getString("url"));
// System.out.println(rs.getString("size"));
// }
//
//
// }catch(SQLException e){
// e.printStackTrace();
// }
//
//
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -