【发布时间】:2012-07-21 06:34:43
【问题描述】:
我有一个名为"Sta" 的课程。
每次我尝试连接到 mysql 数据库时都会遇到异常:
2012/07/23 03:34:50SQLException: 没有为 mysql:jdbc://127.0.0.1:3306/sta?user=root.. 找到合适的驱动程序。 这个异常像 说发生在: this.db_con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/sta?user=root");
到目前为止我所做的是:
- 将mysql驱动jar放入库中(通过项目 属性),
手动将驱动jar放入WEB-INF/lib(创建lib),然后
也放到tomcat的库目录下。
所有结果都相同 (我正在使用:mysql-connector-java-5.1.20-bin.jar)
除上述异常外,项目本身编译部署正常。 此外,如果我对“普通”java - RMI 应用程序使用相同的连接字符串,它可以正常工作并且没有任何故障。
public class Sta_client extends HttpServlet
{
private Connection db_con=null;
public Sta_client() throws ServletException
{
super();
if (this.db_con==null)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try
{
this.db_con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/sta?user=root");
}
catch(SQLException ex)
{
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.err.println(dateFormat.format(date)+"SQLException: " + ex.getMessage());
}
}
}
【问题讨论】:
-
我不确定您为什么会遇到类路径问题,但您不应该在构造函数中创建数据库连接。在 doGet() 方法或更好的方法中执行此操作,设置连接池。
-
有些奇怪,在您的代码中是
jdbc:mysql:...,但在您提供的错误消息中是mysql:jdbc:...,是这样吗?
标签: java mysql jar glassfish netbeans-7