没有为jdbc找到合适的驱动程序:mysql:// localhost:3306 / with netbeans 7.0.1,glassfish 3.1

时间:2012-07-23 01:41:52

标签: java mysql jar glassfish netbeans-7

我有一个名为"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用户=根”);

到目前为止我所做的是:

  • 将mysql驱动程序jar放入库中(通过项目 属性),
  • 手动将驱动程序jar放入WEB-INF / lib(creaded 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());
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

Netbeans 7.0.1有mysql连接器。移除你的mysql-connector-java-5.1.20-bin.jar并添加支持NetBeans IDE的jar。