谁有Servlet访问数据库的完整的程序?

jiupingzi 2003-08-24 12:21:49
我很想了解一下Servlet使用数据库的过程,包括连接,插入数据,查询数据库。都可以。
有的话给小弟发一份,yijiuping_159@163.com
发了的都有分。
...全文
31 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhx_232 2003-08-24
  • 打赏
  • 举报
回复
package ehomework;
import java.sql.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/

public class ConnDb {

public String _user;
public String _durl;
public String _driver;
public String _pass;
Statement _stmt;
Connection _conn=null;

public ConnDb() {
_user="sa";
_pass="sa";
_driver="sun.jdbc.odbc.JdbcOdbcDriver";
_durl="jdbc:odbc:test";
}

public ConnDb(String username,String password,String durl,String driver){
_user=username;
_pass=password;
_durl=durl;
_driver=driver;
}

public Connection myConn(){
try{
Class.forName(_driver);
_conn=DriverManager.getConnection(_durl,_user,_pass);
}catch(Exception e){
System.err.println("connDB's myConn() throw exception:"+e.getMessage());
}
return _conn;
}

public ResultSet execQuery(String sqlstr){
ResultSet rs=null;
try{
Connection rsConn=myConn();
_stmt=rsConn.createStatement();
rs=_stmt.executeQuery(sqlstr);
System.out.println("The SQL Statement is:"+sqlstr);
}catch(SQLException ex){
System.err.println("connDB's execQuery() throw sqlexception:"+ex.getMessage());
System.err.println("The SQL Statement is:"+sqlstr);
}
return rs;
}

public void execUpdate(String sqlstr){
try{
Connection upConn=myConn();
_stmt=upConn.createStatement();
_stmt.executeUpdate(sqlstr);
System.out.println("The SQL Statement is:"+sqlstr);
}catch(SQLException ex){
System.err.println("connDB's execUpdate() throw sqlException:"+ex.getMessage());
System.err.println("The SQL Statement is:"+sqlstr);
}
}

public void myClose(){
try{
_stmt.close();
_conn.close();
}catch(SQLException ex){
System.err.println("connDB's myClose() throw SQLException:"+ex.getMessage());
}
}
}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧