关于JSP连接SQL数据库问题 50分
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%
String account = request.getParameter("account");
String password = request.getParameter("password");
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=book";
Connection conn= DriverManager.getConnection(cont,password);
Statement stmt = con.createStatement();
ResultSet rst = stmt.executeQuery("SELECT * FROM user;");
rst.next();
String rsPassword = rst.getString("password");
if(password.equals(rsPassword)){
response.sendRedirect("user.html");
}
else
response.sendRedirect("index.html");
rst.close();
con.close();
}
catch(Exception e){
out.println(e.getMessage());
}
%>
</body>
</html>
我在输入Index.Html输入了用户名和密码后.加载到这个JSP程序时``想调用数据库里的内容
结果屏幕显示出了 这样一个语句com.microsoft.jdbc.sqlserver.SQLServerDriver 就是把Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").里面的句子打出来了``我想问问这个是怎么回事啊 ```怎么解决啊````或者给出一个正确的调用数据库的方法
问题点数:50、回复次数:9Top
1 楼flyingis()回复于 2005-08-22 15:38:07 得分 10
Connection conn= DriverManager.getConnection(cont,password);
上面这句中cont是不是写错了?Top
2 楼popstare(不务正业)回复于 2005-08-22 15:42:12 得分 0
改后``出现
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 5 in the jsp file: /login.jsp
Generated servlet error:
The method getConnection(String, Properties) in the type DriverManager is not applicable for the arguments (Connection, String)
An error occurred at line: 5 in the jsp file: /login.jsp
Generated servlet error:
con cannot be resolved
An error occurred at line: 5 in the jsp file: /login.jsp
Generated servlet error:
con cannot be resolved
Top
3 楼popstare(不务正业)回复于 2005-08-22 15:43:46 得分 0
原来那个问题解决了````现在又有新的问题了
那位高人帮我解决啊````
救命啊Top
4 楼navy7148(飞尘)回复于 2005-08-22 15:50:20 得分 0
把你改后的代码粘上来,给我look!Top
5 楼jackeyzsu()回复于 2005-08-22 15:51:53 得分 0
The method getConnection(String, Properties) in the type DriverManager is not applicable for the arguments (Connection, String)
不是提示这句错了嘛?Top
6 楼wjs2338(Yesterday Once More)回复于 2005-08-22 16:00:43 得分 40
给你个数据库连接池的文件,每次到页面上去写,不决得烦吗?
我现在用的数据库连接池文件原码如下:
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import java.util.*;
import java.io.*;
/**
* Description:数据库的封装操作
* @author:shiyq
* @version 1.0
*/
public class connpool{
private Context ctx=null;
private Connection conn = null;
private Statement stmt = null; //有回滚的
private PreparedStatement pstmt = null; //@param查询
// Utility ut=new Utility(); //声明一个汉字内码转换的类
public void init() {
try{
ctx=new InitialContext();
// if(ctx==null)
// throw new Exception("没有匹配的环境");
DataSource ds=(DataSource)ctx.lookup("connectDB");
// if(ds==null)
// throw new Exception("没有匹配数据库");
conn=ds.getConnection();//取得连接池
}catch(Exception e){System.out.println("找不到数据源");}
}
/*** 构造数据库的连接和访问类*/
public connpool() throws Exception {
init();
}
//有回滚功能的生成器
public void setStmt() throws Exception {
this.stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}
//可更新结果集的生成器
public void setDStmt() throws Exception {
this.stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
/***返回状态* @return Statement 状态 */
public Statement getStmt() throws Exception {
return stmt;
}
/** * 可回滚的预编译SQL语句 * @param sql SQL语句 */
public void setPstmt(String sql) throws Exception {
this.pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}
/** * 返回预设状态 */
public PreparedStatement getPstmt() throws Exception{
return pstmt;
}
public void clearParameters() throws SQLException {
pstmt.clearParameters();
pstmt=null;
}
/** * 返回连接 * @return Connection 连接 */
public Connection getConnection() {
return conn;
}
/*** 设置对应值 *
* @param index 参数索引
* @param value 对应值
*/
public void setString(int index,String value) throws SQLException {
pstmt.setString(index, value);
}
public void setInt(int index,int value) throws SQLException {
pstmt.setInt(index,value);
}
public void setBoolean(int index,boolean value) throws SQLException {
pstmt.setBoolean(index,value);
}
public void setDate(int index,java.sql.Date value) throws SQLException {
pstmt.setDate(index,value);
}
public void setLong(int index,long value) throws SQLException {
pstmt.setLong(index,value);
}
public void setFloat(int index,float value) throws SQLException {
pstmt.setFloat(index,value);
}
public void setBytes(int index,byte[] value) throws SQLException{
pstmt.setBytes(index,value);
}
public void setTime(int index, java.sql.Time x) throws SQLException{
pstmt.setTime(index,x);
}
public void setTimestamp(int index, java.sql.Timestamp x) throws SQLException{
pstmt.setTimestamp(index,x);
}
public void setAsciiStream(int index,java.io.InputStream x,int length) throws SQLException{
pstmt.setAsciiStream(index,x,length);
}
/*public void setUnicodeStream(int parameterIndex,java.io.InputStream x,int length) throws SQLException{
@Deprecated
pstmt.setUnicodeStream(parameterIndex,x,length);
}*/
public void setBinaryStream(int index,java.io.InputStream x,int length) throws SQLException{
pstmt.setBinaryStream(index,x,length);
}
public void setCharacterStream(int index,java.io.Reader reader,int length) throws SQLException{
pstmt.setCharacterStream(index,reader,length);
}
public void setBlob(int index,Blob x) throws SQLException{
pstmt.setBlob(index,x);
}
public void setClob(int index,Clob x) throws SQLException{
pstmt.setClob(index,x);
}
public void setObject(int index,Object x,int targetSqlType,int scale)throws SQLException{
pstmt.setObject(index,x,targetSqlType,scale);
}
public void setObject(int index,Object x,int targetSqlType)throws SQLException{
pstmt.setObject(index,x,targetSqlType);
}
public void setObject(int index,Object x)throws SQLException{
pstmt.setObject(index,x);
}
/** * 执行一般的SQL语句返回字段集 */
public ResultSet ExeQuery(String sql) throws SQLException {
if (stmt != null) {
return stmt.executeQuery(sql);
}
else return null;
}
//执行有预处理功能的查询
public ResultSet ExeQuery() throws SQLException {
if (pstmt != null) {
return pstmt.executeQuery();
}
else return null;
}
/*
//** * 执行一般的更新SQL语句 *
public void ExeUpdate(String sql) throws SQLException {
if (stmt != null)
stmt.executeUpdate(sql);
}
//执行有预处理功能的更新操作
public void ExeUpdate() throws SQLException {
if (pstmt != null)
pstmt.executeUpdate();
}
*/
public boolean ExeUpdate(String sql) throws SQLException {
boolean flat=false;
if (stmt != null)
if (stmt.executeUpdate(sql)>0) {
flat=true; }
return flat;
}
public boolean ExeUpdate() throws SQLException {
boolean flat=false;
if (pstmt != null)
if (pstmt.executeUpdate()>0){
flat=true; }
return flat;
}
/** * 关闭连接 */
public void close() throws Exception {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
if (conn!=null)
{
try{
conn.close();
conn = null;
}finally
{
if(conn!=null)
conn.close();
}
}
if(ctx!=null)
{
try{
ctx.close();
ctx = null;
}finally
{
if(ctx!=null)
ctx.close();
}
}
}
}
你拷过去,放到记事本里,后缀名改成java就行了Top
7 楼popstare(不务正业)回复于 2005-08-22 18:06:30 得分 0
楼上的大哥```拷贝之后
该怎么用啊``好像是要配置连接池把
还是要编译过后放在那里啊
Top
8 楼popstare(不务正业)回复于 2005-08-22 18:14:05 得分 0
把具体步骤告诉我吧````谢谢了Top
9 楼yeno(人在广州)回复于 2005-08-22 20:54:58 得分 0
你去网上随便搜搜数据库连接Bean,一大把的,数据库连接池要配置tomcat里conf目录下的的server.xml和context.xml两个文件,具体配置,如果楼主没在网上找到的话,就给我留言!Top




