Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java连接到mysql?_Java_Mysql - Fatal编程技术网

如何使用java连接到mysql?

如何使用java连接到mysql?,java,mysql,Java,Mysql,我想把来自Gmail的电子邮件存储到我的mysql数据库中。 我在google上找到Inboxreader,但连接mysql的部分不起作用。 用户名、数据库名称和密码正确无误 有人能帮我吗。 多谢各位 这是代码的一部分 { Properties details= new Properties(); details.load(new FileInputStream("details.properties")); String

我想把来自Gmail的电子邮件存储到我的mysql数据库中。 我在google上找到Inboxreader,但连接mysql的部分不起作用。 用户名、数据库名称和密码正确无误

有人能帮我吗。 多谢各位

这是代码的一部分

{
            Properties details= new Properties();
            details.load(new FileInputStream("details.properties"));
            String userName = details.getProperty("root");
            String password = details.getProperty("password");
            String url = details.getProperty("jdbc:mysql://localhost/test");
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
            PreparedStatement st= conn.prepareStatement("insert into 'Email_list' values(?)");
            for(String mail:mails)
            {
            try{
                  st.setString(1, mail);
                  st.execute();
                }catch(Exception e){}
            }



        }
        catch (Exception e)
        {
            System.err.println ("Cannot connect to database server");
            e.printStackTrace();
        }
        finally
以下是错误代码:

Cannot connect to database server
java.sql.SQLException: The url cannot be null
Reading:23
    at java.sql.DriverManager.getConnection(DriverManager.java:554)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at inboxreader.InboxReader.connecttoMySql(InboxReader.java:181)
    at inboxreader.InboxReader.Start(InboxReader.java:82)
    at inboxreader.InboxReader.main(InboxReader.java:34)
谢谢

这是您的问题:

String url = details.getProperty("jdbc:mysql://localhost/test");
您在
url
中得到一个
null
值。这是因为没有名为
jdbc的属性:mysql://localhost/test
在您的属性文件中

你有两个选择。我们可以直接将
url
与以下内容结合使用:

String url = "jdbc:mysql://localhost/test";
另一个选项是在
详细信息中正确设置属性。属性

# hello, I am details.properties file
jdbc.url=jdbc:mysql://localhost/test
然后,在Java代码中,您可以从如下属性读取
url

String url = details.getProperty("jdbc.url"); // note that we're changing property name
String url = details.getProperty("jdbc:mysql://localhost/test");

您试图从
details
获取属性值,如下所示:

String url = details.getProperty("jdbc.url"); // note that we're changing property name
String url = details.getProperty("jdbc:mysql://localhost/test");

在我看来,那里的属性名称实际上就是您的值。

这是因为您没有一个键“jdbc:mysql://localhost/test“在您的属性文件中。假设details.properties包含以下内容:

url=jdbc:mysql://localhost/test
所以你的代码应该是

String url = details.getProperty("url");

我确信您的财产密钥存在以下问题:

 String url = details.getProperty("jdbc:mysql://localhost/test");
您应该首先验证您的密钥是否正确

 if (details.getProperty("jdbc:mysql://localhost/test") != null || 
    details.getProperty("jdbc:mysql://localhost/test").trim().length > 0){
       url =details.getProperty("jdbc:mysql://localhost/test");
 }else{
 return new Exception("Wrong property key");
 }

@Pacific—也没有理由将Java代码绑定到MySQL。因为您已经有了
details.properties
放入类似
jdbc.url=jdbc的内容:mysql://etc
jdbc.driver=com.mysql.jdbc.driver
然后您可以执行
Class.forName(details.getProperty(“jdbc.driver”)
非常感谢Pablo先生,Sergio和fivedigitNow我的代码正在工作,但我犯了一个错误,我知道数据库名是test,表名是Email_list,但当我转到mysql时,表名是Email_list是空的。“当我转到mysql时,表名是Email_list是空的”意味着当你使用mysql ide时?或者当您建立连接时?,