一个jsp连接数据库(mssql)的问题,老是解决不了,真急人啊!请来帮帮忙!
********************1、数据库连接源文件:db.java ***********************
package stu;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class db
{
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:stu";
Connection conn=null;
public void opendb()
{
try
{
Class.forName(sDBDriver);
}catch(ClassNotFoundException cnfe)
{
System.err.println("Error loading driver: "+ cnfe);
}
}
public ResultSet executeQuery(String sql)
{
ResultSet rs=null;
try
{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException sqle)
{
System.err.println("Error connectiong: "+sql);
}
return rs;
}
}
******************2、servlet处理源文件:addNewBBS.java ******************
package stu;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class addNewBBS extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
String id=request.getParameter("id");
String title=request.getParameter("newTitle");
String content=request.getParameter("Content");
/*连接数据库*/
db mydb=new db();
mydb.opendb();
String sql="insert bbs(nvTitle,nvData,ncAuthor,ncProp) values('"+title+"','"+content+"','"+id+"','1')";
ResultSet rs=null;
rs=mydb.executeQuery(sql);
}
}
*****************3、jsp源文件:newBBS.jsp?id=009851216 *****************
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="author" content="Kevin Shi">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>New BBS</title>
</head>
<body bgcolor="#fdf5e6">
<%
String id=request.getParameter("id");
%>
<form action="/stu/servlet/stu.addNewBBS" method="post">
<div align="center">
<center>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#FDF5E6" width="100%" height="223">
<tr>
<td width="10%" height="28"><b>主题:</b></td>
<td width="90%" height="28"><input type="text" name="newTitle" size="85"></td>
</tr>
<tr>
<td width="10%" height="173"><b>内容:</b><p> </td>
<td width="90%" height="173" valign="top">
<textarea cols="80" rows="10" name="Content"></textarea><br>
<input type="submit" value="发 表" border="0" name="B1" WIDTH="60" HEIGHT="20"> <INPUT name="B2" TYPE="reset" WIDTH="60" HEIGHT="20" border="0" value="重 写"><br>
</td>
</tr>
<tr>
<td width="10%" height="17"></td>
<td width="90%" height="17" align="right">
<b><font size="2" color="#FF00FF"><a href=javascript:history.go(-1)>返回</a></font></b></td>
</tr>
</table>
</center>
</div>
<input type="hidden" name="id" value=id>
</form>
</body>
</html>