?? 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 + -