求一个jsp使用import一个类进行数据库连接的例子

guanxf 2003-04-09 09:13:51
不是使用javabean,最好类文件和jsp文件都贴出来cc
...全文
8 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
zxhong 2003-04-09
http://www.cn-java.com/target/news.php?news_id=2171
回复
zxhong 2003-04-09
http://expert.csdn.net/Expert/topic/1609/1609345.xml?temp=.8218042
回复
moumouren 2003-04-09
/* DBcon.java */
import java.util.*;
import java.io.*;
import java.sql.*;

public class DBcon
{
String DBDriver = "oracle.jdbc.driver.OracleDriver"; //driver address;
String ConnStr = "jdbc:oracle:thin:@dataserver:1521:oradb";
String User="test";//database user name
String Passwd="test";//user password

Connection conn = null; //connection
Statement stmt = null; //statment
ResultSet rs = null; //resultset

public void DBcon (){
try {
System.out.print("test");
Class.forName(DBDriver).newInstance();//load db driver
conn = DriverManager.getConnection(ConnStr,User,Passwd);//create connection;
}catch(java.lang.ClassNotFoundException e) {
System.err.println("DBcon (): " + e.getMessage());
}catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}catch (Exception e)
{
System.err.println("\n打开数据库时异常,错误信息为:\n" + e.getMessage());
}
}

//执行查询,返回数据集;
public ResultSet query(String sql) {

rs = null;
try {
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);//create statment
rs = stmt.executeQuery(sql);
}catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;

}
//执行更新,返回操作状态;
public int update(String sql) {
int success_update=0;
try {
Statement stmt = conn.createStatement();//create statment
success_update=stmt.executeUpdate(sql);
}catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return success_update;
}

//关闭连接;
public void conClose(){
try {
conn.close();
}catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
}
}


/*jsp*/
<%
DBcon db = new DBcon();
%>
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2003-04-09 09:13
社区公告
暂无公告