📄 db.java
字号:
package pubTools;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
import java.io.*;
public class db {
private Connection con;
private Statement stmt;
private ResultSet rs;
public db() {
con = null;
stmt = null;
rs = null;
}
//定义结果集执行查询操作
public ResultSet executeQuery(String sql) {
rs = null;
try {
Context initCtx = new javax.naming.InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/kcsj");
con = ds.getConnection();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
//数据库连接
rs = stmt.executeQuery(sql);
//将SQL执行查询操作结果,存到rs}
catch(NamingException ex) {
System.err.println("db.lookupName: " + ex.getMessage());
}
catch(SQLException ex) {
System.err.println("db.executeQuery: " + ex.getMessage());
}
return rs;
// 返回所请求的页面
}
public void executeUpdate(String sql) {
try {
Context initCtx = new javax.naming.InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/kcsj");
con = ds.getConnection();
stmt = con.createStatement();
stmt.executeUpdate(sql);
stmt.close();
con.close();
}
catch(NamingException ex) {
System.err.println("db.lookupName: " + ex.getMessage());
}
catch(SQLException ex) {
System.err.println("db.executeUpdate:" + ex.getMessage());
}
}
public void closeCon(){
if(rs != null){
try{
rs.close();
}catch(SQLException e){e.printStackTrace();}
rs = null;
}
if(stmt != null){
try{
stmt.close();
}catch(SQLException e){e.printStackTrace();}
stmt = null;
}
if(con != null){
try{
con.close();
}catch(SQLException e){e.printStackTrace();}
con = null;
}
}
//用来显示中文字体;
public String getStr(String str) {
try {
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e) {
e.printStackTrace();
}
return "null";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -