加入收藏 | 设为首页 | 会员中心 | 我要投稿 辽源站长网 (https://www.0437zz.com/)- 云专线、云连接、智能数据、边缘计算、数据安全!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

如何使用java连接到mysql?

发布时间:2021-04-03 13:15:32 所属栏目:MySql教程 来源:网络整理
导读:我想将Gmail中的电子邮件存储到我的mysql数据库中. 我发现有谷歌的Inboxreader,但连接到mysql的部分无法正常工作. 用户名,数据库名称,密码是否正确. 谁能帮我. 谢谢. 这是代码的一部分 { Properties details= new Properties(); details.load(new FileInputS

我想将Gmail中的电子邮件存储到我的mysql数据库中.
我发现有谷歌的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中获得空值.那是因为在属性文件中没有名为jdbc:mysql:// localhost / test的属性.

你有两个选择.一个人会直接使用url,例如:

String url = "jdbc:mysql://localhost/test";

另一个选项是在details.properties中具有正确设置的属性:

# 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

(编辑:辽源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读