加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

如何使用java连接到mysql?

发布时间:2021-04-05 18:41:05 所属栏目:编程 来源:网络整理
导读:我想将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

(编辑:湖南网)

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

    热点阅读