📄 dbconnectionbean.java
字号:
//DBConnectionBean.java
//用在servlet里的数据库桥接bean
import java.util.*;
import java.sql.*;
import java.io.*;
public class DBConnectionBean{
Connection dbcon=null;
Statement stmt=null;
ResultSet result=null;
String driver="";
String url="";
String user="";
String password="";
public DBConnectionBean(){
try{
InputStream fis =getClass().getResourceAsStream("jdbcsql.properties");
Properties ps=new Properties();
ps.load(fis);
driver=ps.getProperty("driver");
url=ps.getProperty("url");
user=ps.getProperty("username");
password=ps.getProperty("password");
}
catch(Exception e){
System.out.println(e);
}
try{
Class.forName(this.driver);
System.out.println("加载数据驱动成功!");
}catch(ClassNotFoundException e){
System.out.println("jdbc driver error");
}
}
public Connection openConnection(){
try{
this.dbcon=DriverManager.getConnection(this.url,this.user,this.password);
System.out.println("桥接数据库成功!");
}catch(SQLException e2){
System.out.println(e2);
}
return dbcon;
}
public ResultSet executeQuery(String query)throws SQLException{
this.stmt=dbcon.createStatement();
this.result=stmt.executeQuery(query);
return result;
}
public void executeUpdate(String query)throws SQLException{
this.stmt=dbcon.createStatement();
stmt.executeUpdate(query);
if(stmt!=null) stmt.close();
}
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 boolean next() throws SQLException{
return result.next();
}
public void resetResult() throws SQLException{
this.result=null;
}
public void close() throws SQLException{
if(dbcon!=null) dbcon.close();
if(stmt!=null) stmt.close();
if(result!=null) result.close();
}
public void finalize() throws Throwable{
this.close();
}
public String replaceString(String str){
char tempArray[]=str.toCharArray();
int iCurr[]=new int[10];
int j=0;
for(int i=0;i!=tempArray.length;i++)
{
int temp=(int)tempArray[i];
if(temp==92){
iCurr[j]=i;
j++;
}
}
final int temp2=j+1;
String strArray[]=new String[temp2];
strArray[0]=str.substring(0,iCurr[0]);
strArray[temp2-1]=str.substring(iCurr[j-1]+1);
for(int m=1;m!=strArray.length-1;m++){
strArray[m]=str.substring(iCurr[m-1]+1,iCurr[m]);
}
str="";
for(int k=0;k!=strArray.length-1;k++){
str=str+strArray[k]+"%5C";
}
str=str+strArray[temp2-1];
return str;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -