jsp与mysql的连接

mht100 2004-09-29 11:24:39
tomvat5.3下作的Jsp网站如何与mysql的数据库相连呢?(已经写好class文件)谢谢!
...全文
57 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xharry 2004-10-12
  • 打赏
  • 举报
回复
这个是一个javabean,jsp用的

package DBase;
import java.sql.*;
public class Conn{
String DBDriver="org.gjt.mm.mysql.Driver";
String ConnStr="jdbc:mysql://localhost/netkiss?useUnicode=true&characterEncoding=ISO-8859-1";
String MyUser="root";
String MyPassword="";
Connection conn = null;
ResultSet rs = null;
/*****************************************************************
*函数名 :connect
*功能 :连接数据库
*作成日 :28/04/2003
*****************************************************************/
public void connect(){
try{
Class.forName(DBDriver);
}
catch(java.lang.ClassNotFoundException e)
{System.err.println("DBconn (): " + e.getMessage());
}
}

/*****************************************************************
*函数名 :executeUpdate
*功能 :插入、更新数据库
*作成日 :28/04/2003
*****************************************************************/
public int executeUpdate(String sql){
int update=-1;
try{
conn = DriverManager.getConnection(ConnStr,MyUser,MyPassword);
Statement stmt = conn.createStatement();
update = stmt.executeUpdate(sql);
}
catch(SQLException ex)
{System.err.println("aq.executeQuery:"+ex.getMessage());
}
return update;
}
/****************************************************************
*函数名 :executeQuery
*功能 :查询数据库
*作成日 :28/04/2003
****************************************************************/
public ResultSet executeQuery(String sql){
rs = null;
try{
conn = DriverManager.getConnection(ConnStr,MyUser,MyPassword);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{System.err.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}


/***************************************************************
*函数名 :disconnect()
*功能 :断开数据库连接
*作成日 :28/04/2003
***************************************************************/
public void disconnect(){
try{
if (conn!=null){
conn.close();
}
}catch (SQLException sqle){
System.err.println(sqle.getMessage());
}

}
}
evener 2004-10-11
  • 打赏
  • 举报
回复
选一个合适你的^_^
evener 2004-10-11
  • 打赏
  • 举报
回复
先下载安装JDBC驱动程序。有4种,在http://industry.java.sun.com/products/jdbc/drivers可以找到并下载。

23,118

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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