【问题标题】:Connecting to MySQL Database from java in IDE Netbeans在 IDE Netbeans 中从 java 连接到 MySQL 数据库
【发布时间】:2013-11-28 13:30:02
【问题描述】:

所以这是我的事。我正在尝试从 java 连接做 MySQL 数据库。我已经从 MySQL 下载了连接器驱动程序(它称为 "mysql-connector-java-5.0.8-bin.jar" )并添加到我的库中(我使用的是 NetBeans)。我试着做这样的简单连接:

package datacon;

public class Datacon {

    public static void main(String[] args) {

        try {

            java.sql.Connection con = (java.sql.Connection) java.sql.DriverManager
                .getConnection("jdbc:mysql://localhost:3306/test"
                /*, "root"
                  , "root" */ );

        } catch ( Exception e ) {
            System.out.println( e.toString() );
        }
    }
}

但这已经发生了:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test

我在互联网上进行了研究,这很常见,但没有一个答案对我有帮助。我能够通过 NetBeans/services 连接到数据库,因此 URL jdbc:mysql://localhost:3306/test 应该是正确的。

我正在使用:

java:   Oracle java SDK 1.7.0 _ 45
IDE:    NetBeans 7.4
OS:     Debian 3.2.51-1 x86_64
Driver: MySQL Connector/J 5.0.8

恐怕这会有一个非常琐碎的答案,但我现在被困在这里一段时间了,我需要搬家。那么我错过了什么?

【问题讨论】:

    标签: java mysql netbeans jdbc


    【解决方案1】:

    添加:

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (final ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

    • 静态不需要。作为解释:驱动类是通过这种方式加载的,因此不需要导入特定的数据库提供程序,并且 API 不能使用特定的提供程序功能进行扩展。
    【解决方案2】:

    你忘记加载驱动程序了。

    Class.forName("com.mysql.jdbc.Driver");
    

    并确保您的课程路径中有mysql-connector-java-5.1.22-bin

    【讨论】:

      【解决方案3】:
      try {
            Class.forName("org.gjt.mm.mysql.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
          } catch(ClassNotFoundException e) {
              e.printStackTrace();
          }
      

      【讨论】:

        【解决方案4】:

        试试{ class.forname("com.mysql.jdbc.Driver");

             driver.getconnection("connenctionurl","name","password");
        
                    Statement s =goodrecive.asif().createStatement();
                    s.executeUpdate("INSERT INTO product(productno,productname,quantity,ammount)values('"+t1.getText()+"','"+t2.getText()+"','"+t3.getText()+"','"+t4.getText()+"')");
        
            } catch (Exception e) {
                System.out.println(e);
            }
        }                         
        

        【讨论】:

          【解决方案5】:

          我的连接类:

          package connection;
          
          import java.sql.Connection;
          import java.sql.DriverManager;   
          import java.sql.SQLException;
          
          public class ConnectionFactory {  
              public Connection getConnection() throws SQLException {
                  return DriverManager.getConnection("jdbc:mysql://localhost:3306/<database>", "<user>", "<password>");
              }        
          }   
          

          不需要添加 ClassForName。这在旧版本的 java 中是必需的。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-18
            • 1970-01-01
            • 1970-01-01
            • 2015-12-13
            • 2017-02-03
            • 1970-01-01
            相关资源
            最近更新 更多