在 IDE Netbeans 中从 java 连接到 MySQL 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20267511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:42:53  来源:igfitidea点击:

Connecting to MySQL Database from java in IDE Netbeans

javamysqlnetbeansjdbc

提问by Addman

So this is my thing. I am trying to connect do MySQL Database from java. I have downloaded connector driver from MySQL ( Its called "mysql-connector-java-5.0.8-bin.jar") and I added to my libraries (Im using NetBeans). I tried to do simple connection like this:

所以这是我的事。我正在尝试从 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() );
        }
    }
}

But this hapened:

但这发生了:

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

I made my research on the internet and this pretty common thing, but none of the answers helped me. I was able connect to database through NetBeans/services, so that URL jdbc:mysql://localhost:3306/testshould be correct.

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

I am using:

我在用:

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

I am afraid this is gonna have a very trivial answer, but I am stuck here for a while now and i need to move. So what am I missing?

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

采纳答案by Paul Vargas

Add:

添加:

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

回答by KhAn SaAb

I thing you forget to load driver .

我的事情你忘记加载驱动程序。

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

and make sure you have mysql-connector-java-5.1.22-binin your class path.

并确保你mysql-connector-java-5.1.22-bin在你的班级路径中。

回答by Tatarao voleti

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

回答by user3441712

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

试试 { 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);
    }
}                         

回答by Tiago NET

My class for connection:

我的连接类:

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>");
    }        
}   

Don't need add ClassForName. This is necessary in older versions of java.

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