
java连接mysql,这段代码错误在哪里呢?求指点~
db.properties配置文件:driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/booksto...
db.properties配置文件:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/bookstore
uid=root
pwd=123456
然后是数据库连接类:
package com.ztq;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DBServer {
public static Connection conn = null;
public static synchronized Connection getConnection() throws Exception {
Properties pro = new Properties();
try {
pro.load(DBServer.class.getResourceAsStream("/db.properties"));
String driver = pro.getProperty("driver");
String url= pro.getProperty("url");
String uid= pro.getProperty("uid");
String pwd= pro.getProperty("pwd");
Class.forName(driver);
conn = DriverManager.getConnection(url,uid,pwd);
} catch (SQLException ex) {
throw new RuntimeException("fail", ex);
} catch (ClassNotFoundException ex) {
throw new RuntimeException("fail", ex);
} catch (IOException ex1) {
ex1.printStackTrace();
throw new RuntimeException("fail", ex1);
}
return conn;
}
}
jsp中使用的语句是这样:
Connection conn=DBServer.getConnection();
报错如下:
An error occurred at line: 5 in the jsp file: /showarticletree.jsp
DBServer cannot be resolved to a type
4: <%
5: Connection conn=DBServer.getConnection();
6:
7: Statement stmt=conn.createStatement();
8: ResultSet rs=stmt.executeQuery("select * from article"); 展开
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/bookstore
uid=root
pwd=123456
然后是数据库连接类:
package com.ztq;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DBServer {
public static Connection conn = null;
public static synchronized Connection getConnection() throws Exception {
Properties pro = new Properties();
try {
pro.load(DBServer.class.getResourceAsStream("/db.properties"));
String driver = pro.getProperty("driver");
String url= pro.getProperty("url");
String uid= pro.getProperty("uid");
String pwd= pro.getProperty("pwd");
Class.forName(driver);
conn = DriverManager.getConnection(url,uid,pwd);
} catch (SQLException ex) {
throw new RuntimeException("fail", ex);
} catch (ClassNotFoundException ex) {
throw new RuntimeException("fail", ex);
} catch (IOException ex1) {
ex1.printStackTrace();
throw new RuntimeException("fail", ex1);
}
return conn;
}
}
jsp中使用的语句是这样:
Connection conn=DBServer.getConnection();
报错如下:
An error occurred at line: 5 in the jsp file: /showarticletree.jsp
DBServer cannot be resolved to a type
4: <%
5: Connection conn=DBServer.getConnection();
6:
7: Statement stmt=conn.createStatement();
8: ResultSet rs=stmt.executeQuery("select * from article"); 展开
2个回答
展开全部
这说明这个jsp页面在被解析为servlet的时候不认识这个DBServer类,首先你确定你的jsp页面已经引入了DBServer类即:<%@ipage import=“*.*.DBServer”%> 然后我再给你解答
追问
嗯,我确定引入啦~麻烦再看下。。
追答
你的问题解决了你db.properties文件位置放得不对,DBServer.class.getResourceAsStream("/db.properties")这个方法中db.properties文件应该放在src的直接根目录下,而不是整个应用程序的根目录
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询