我写了一个小网站,我把连接数据库封装到两个类中,方法也封进去了,放在WEB-INF/classes下
具体是这么写的DBConnectionManager.javapackage bookshop;
import java.sql.*;
public class DBConnectionManager{
private String DriverName = 
"org.gjt.mm.mysql.Driver";
private String url =
"jdbc:mysql://localhost/jspshop?useUnicode=true&characterEncoding=GB2312";
private String user="root";
        private String password="1983113";
public void setDrverName(String newDriverName){
this.DriverName = newDriverName;
}
public String getDriverName(){
return DriverName;

}
public void setUrl(String newUrl){
this.url=newUrl;
}
public String getUrl(){
return url;
}
public void setUser(String newUser){
this.user = newUser;
}
public String getUser(){
return user;
}
public void setPassword(String newPassword){
this.password=newPassword;
}
public String getPassword(){
return password;
}

public Connection getConnection(){
try
{
Class.forName(DriverName).newInstance();
return DriverManager.getConnection(url,user,password);
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
DBSQLManager.java
package bookshop;import java.sql.*;
import bookshop.*;public class DBSQLManager{
protected Connection conn=null; 
protected Statement stmt=null;
protected ResultSet rs=null;
protected String sqlStr;public DBSQLManager(){
try
{
sqlStr="";
DBConnectionManager dcm=new DBConnectionManager();
conn=dcm.getConnection();
stmt=conn.createStatement();
}
catch(Exception e){
System.out.println(e);
isConnect=false;
}
}
public Statement getStmt(){
return stmt;
}
public Connection getConn(){
return conn;
}
public ResultSet getRs(){
return rs;
}
public void setSqlStr(String newSqlstr){
this.sqlStr=newSqlStr;
}
public String getSqlStr(){
return sqlStr;
}
public void executeQuery() throws Exception {
rs=stmt.executeQuery(sqlStr);
}
public void executeUpdate() throws Exception {
stmt.executeUpdate(sqlStr);
}
public void close() throws SQLException {
if(stmt !=null){
stmt.close();
stmt=null;
}
conn.close();
conn=null;
}
}
在JSP页面中我将DBSQLManager实例化(DBSQLManager dbsm=new DBSQLManager();
然后用实例对象调用方法对数据库进行插入,查询等操作,但是每次要么是页面完全通过,但是数据并没有插入进去,要么就是显示找不到类.各位大哥请指教~~~

解决方案 »

  1.   

    楼上的,打包重不重名有什么关系呢。重名还能省个import多好。楼主,如果
    1。你两个都放在class文件夹,
    2。包名相同,
    那么第二个类不要写import bookshop.*;其他的来研究一下。
    DBSQLManager.java居然还有2个编译错误,一个变量未定义,一个大小写错误
      

  2.   

    有import 那个类吗
    引用了才可以实例化
      

  3.   

    其它的没看出错误来。
    会不会是sql语句拼错了?你没贴上来不好判断。