请问怎样用javabean封装连接mySQL数据库?

ypl 2004-07-23 03:21:54
我刚开始学习jsp还不太熟悉,觉得在每一个页面都连接一次数据库不怎么方便,请问怎样用javabean封装连接mySQL数据库,最好能够给贴个实例。
...全文
370 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
CNetol 2004-07-29
  • 打赏
  • 举报
回复
需要有个包!
package database;

database就是包
你必须放在myapp/WEB-INF/classes/database
ypl 2004-07-26
  • 打赏
  • 举报
回复
我还有个问题想请教以上各位,我在tomcat/webapps下建了个虚拟目录myapp,我把bean放在这个虚拟目录下的classes下即:myapp/WEB-INF/classes下,可是现在调用bean时出错,不知道是不是bean的位置放的是否正确??
miwu 2004-07-23
  • 打赏
  • 举报
回复
呵呵
前两天写了一个带连接池的
支持外部文件设置连接的那种
要不要?
比较庞大,还有内隐类

不过不知道连接池写得怎么样
CNetol 2004-07-23
  • 打赏
  • 举报
回复
package database;
import java.sql.*;
public class DBconn
{String DBDriver="org.gjt.mm.mysql.Driver";
String ConnStr="jdbc:mysql://localhost/data";
String MyUser="root";
String MyPassword="";
Connection conn = null;
Statement stmt=null;
ResultSet rs = null;
public DBconn()
{try
{Class.forName(DBDriver);
}
catch(java.lang.ClassNotFoundException e)
{System.err.println("DBconn (): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{rs = null;
try
{conn = DriverManager.getConnection(ConnStr,MyUser,MyPassword);
Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{System.err.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql){
{
try
{conn = DriverManager.getConnection(ConnStr,MyUser,MyPassword);
Statement stmt = conn.createStatement();
stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{System.err.println("aq.executeQuery:"+ex.getMessage());
}
}
}
public boolean closeConn()
{
try
{
if (rs!=null) rs.close();
if (stmt!=null) stmt.close();
if (conn!=null) conn.close();
return true;
}
catch ( SQLException ex )
{
System.err.println("closeConn: " + ex.getMessage());
return false;
}
}
}
clingingclinging 2004-07-23
  • 打赏
  • 举报
回复
package practice;
import java.sql.*;
import java.lang.*;
public class DbLink
{
String Sa,Password;
Connection Conn=null;
String StrURL="jdbc:odbc:";
String ConnDriver="sun.jdbc.odbc.JdbcDriver";
Statement Stmt=null;
ResultSet rs=null;
public void DbLink(String sa,String password, String URL1)
{StrURL+=URL1;
Sa=sa;
Password=password;
try{
Class.forName(ConnDriver);
}
catch(ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
}
public void DbExcute(String StrSQL)
{
try
{
Conn=DriverManager.getConnection(StrURL,Sa,Password);
Stmt=Conn.createStatement();
Stmt.executeUpdate(StrSQL);
}
catch(SQLException e)
{
System.err.println(e.getMessage());
}
}
public String getstr()
{
return "hello";
}
}


aqining 2004-07-23
  • 打赏
  • 举报
回复
use the connection pool!
  • 打赏
  • 举报
回复
倒入相应的sql包里的类,写一个public方法返回Connection对象
import java.sql.*;

public class jdbcProxy {
Connection conn = null;
...........

public boolean getConnection() {
bollean tag = true;
try {
Class.forName(".....");
.....
conn = DriverManager.getConnection(..........);

} catch ( Exception e ) {
e.printStackTrace();
tag = false;
}
return tag;
}
..........
}
POwner 2004-07-23
  • 打赏
  • 举报
回复
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch(Exception ex){
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("error.conn.database"));}
try{
String url = "jdbc:mysql://localhost/tifs?user=root&password=´´&useUnicode=true&characterEncoding=8859_1";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);

conn.close();
}
catch(Exception ex){

}

81,122

社区成员

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

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