package net.javafan.guestbook.dboperator;
import java.sql.*;
import javax.sql.*;public class DBConn{
String sDBDriver=""oracle.jdbc.driver.OracleDriver"";
String sConnStr="jdbc:oracle:thin:@localhost:1521:orcl";
Connection conn=null;
String user="scott";
String password="tiger";
String url="jdbc:oracle:thin:@localhost:1521:orcl";
ResultSet rs=null;
public DBConn(){
try{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e){
System.out.println("Jdbc_conn():"+e.getMessage());
}
}
public void executeUpdate(String sql)throws Exception{
try{
conn=DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
conn.close();
stmt.close();
}
catch(SQLException ex){
System.out.println("sql.executeUpdate:"+ex.getMessage());
}
}
public ResultSet executeQuery(String sql)throws Exception{
rs=null;
try{
sql=new String(sql.getBytes("GBK"),"ISO8859_1");
conn=DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex){
System.out.println("sql.executeQuery:"+ex.getMessage());
}
return rs;
} public void CloseConn()
throws Exception {
try
{
if (rs!=null)
rs.close();
if (conn!=null)
conn.close();
}
catch(SQLException ex)
{
System.out.println ("closeConn: " + ex);
}
}
}连接ORACLE在JSP和在STRUTS没什么不同,只要把它写成一个.JAVA,放到ACTION的 TYPE里引一下就好啦
import java.sql.*;
import javax.sql.*;public class DBConn{
String sDBDriver=""oracle.jdbc.driver.OracleDriver"";
String sConnStr="jdbc:oracle:thin:@localhost:1521:orcl";
Connection conn=null;
String user="scott";
String password="tiger";
String url="jdbc:oracle:thin:@localhost:1521:orcl";
ResultSet rs=null;
public DBConn(){
try{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e){
System.out.println("Jdbc_conn():"+e.getMessage());
}
}
public void executeUpdate(String sql)throws Exception{
try{
conn=DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
conn.close();
stmt.close();
}
catch(SQLException ex){
System.out.println("sql.executeUpdate:"+ex.getMessage());
}
}
public ResultSet executeQuery(String sql)throws Exception{
rs=null;
try{
sql=new String(sql.getBytes("GBK"),"ISO8859_1");
conn=DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex){
System.out.println("sql.executeQuery:"+ex.getMessage());
}
return rs;
} public void CloseConn()
throws Exception {
try
{
if (rs!=null)
rs.close();
if (conn!=null)
conn.close();
}
catch(SQLException ex)
{
System.out.println ("closeConn: " + ex);
}
}
}连接ORACLE在JSP和在STRUTS没什么不同,只要把它写成一个.JAVA,放到ACTION的 TYPE里引一下就好啦
你可以将连接数据库做成一个实体类,然后调用他就可以了
其实好的功能你都可以做成单独的类,便于以后复用
将这些类放在一个包中