如何使用java连接到mysql?

前端之家收集整理的这篇文章主要介绍了如何使用java连接到mysql?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我想将Gmail中的电子邮件存储到我的mysql数据库中.
我发现有谷歌的InBoxreader,但连接到MysqL的部分无法正常工作.
用户名,数据库名称,密码是否正确.

谁能帮我.
谢谢.

这是代码的一部分

@H_502_12@{ 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

这是错误代码

@H_502_12@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)

谢谢

最佳答案
这是你的问题:

@H_502_12@String url = details.getProperty("jdbc:MysqL://localhost/test");

您在url中获得空值.那是因为在属性文件中没有名为jdbc:MysqL:// localhost / test的属性.

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

@H_502_12@String url = "jdbc:MysqL://localhost/test";

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

@H_502_12@# hello,I am details.properties file jdbc.url=jdbc:MysqL://localhost/test

然后,在您的Java代码中,您将从属性中读取url,如下所示:

@H_502_12@String url = details.getProperty("jdbc.url"); // note that we're changing property name

猜你在找的MySQL相关文章