类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
我用的bean如下!
直接测试运行可以连接数据库!
我把class和db.properties文件放在了WEB-INF\classes\com\db目录下
运行错误提示:
javax.servlet.ServletException: com/db/DbConnection (wrong name: DbConnection)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:244)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
请教高手!
jsp调用如下:
...
<jsp:useBean id="db" scope="page" class="com.db.DbConnection"/>
<% String sql="select * from t_user where U_Name=\"+name+"\ and U_PW=\"+pass+"\";
db.openConnection();
db.execQuery(sql)
%>
/////////////////////////////////
//bean
///////////////////////////////
import java.util.*;
import java.sql.*;
import java.io.*;
public class DbConnection {
Connection con = null;
Statement stmt = null;
ResultSet rset = null;
int i = 1;
public DbConnection(){
i = 2;
}
/**********************************************************
name:openConnection
param:null
return type:bollean
describe:open database
***********************************************************/
public boolean openConnection(){
//STEP1:载入Property文件
//System.out.println("i="+i);
Properties prop = new Properties();
try{
InputStream is= getClass().getResourceAsStream("db.properties");
prop.load(is);
if(is!=null)
is.close();
}catch(IOException e){
System.out.println(e.toString());
}
//STEP2:读取Property文件
//System.out.println("读取Property文件");
String jdbc = prop.getProperty("driver");
String url = prop.getProperty("url");
String user = prop.getProperty("user");
String password = prop.getProperty("password");
System.out.println("user:"+user);
System.out.println("password:"+password);
System.out.println("driver:"+jdbc);
System.out.println("url:"+url);
//加载JDBC
try{
Class.forName(jdbc);
//Class.forName("net.sourceforge.jtds.jdbc.Driver");
}catch(Exception e){
System.out.println("schmis");
e.printStackTrace();
System.out.println(e.toString());
return false;
}
//打开数据库
try{
this.con = DriverManager.getConnection(url,user,password);
}catch(Exception e){
System.out.println("schmis");
e.printStackTrace();
return false;
}
return true;
}
public void GetStatement() throws SQLException{
stmt = con.createStatement();
//return stmt;
}
/************************************************
name:execQuery
param:query
return type:ResultSet
describe:query database using(SELECT)
**************************************************/
public ResultSet execQuery(String query) throws SQLException{
try{
this.stmt = con.createStatement();
if (con == null)
System.out.println("error");
}catch(Exception e){
System.out.println(e.toString());
}
try{
System.out.println("begin");
this.rset = stmt.executeQuery(query);
System.out.println("end");
//if(stmt!=null) stmt.close();
}catch(Exception e){
System.out.println(e.toString());}
return rset;
}
/*************************************
通过column 名称获取数据
*************************************/
public String getData(String name) throws SQLException{
String data = rset.getString(name);
if(data!=null)
data.trim();
else
data="null";
return data;
}
/*************************************
通过column index 获取数据
*************************************/
public String getData(int index) throws SQLException{
String data = rset.getString(index);
if(data!=null)
data.trim();
else
data="null";
return data;
}
/************************************************
name:execUpdate
param:update
return type:void
describe:update database using(DELETE,UPDATE,INSERT)
**************************************************/
public void execUpdate(String update) throws SQLException{
this.stmt = con.createStatement();
stmt.executeUpdate(update);
if(stmt!=null) stmt.close();
}
/*************************************
将指针移到下一记录
*************************************/
public boolean next() throws SQLException{
return rset.next();
}
public void close() throws SQLException{
if (con!=null) con.close();
if (stmt!=null) stmt.close();
if (rset!=null) rset.close();
}
public static void main(String args[]){
try{
DbConnection db = new DbConnection();
if(db.openConnection()){
System.out.println("schmis");
db.execQuery("select * from t_dorm");
//db.next();
System.out.println("success!");
}
}catch(Exception e){
System.out.println("failed!");
e.printStackTrace();
}
}
}
网友回答:
在DbConnection.java的文件头上加一句:
package com.db;
重起服务器