我已经查看了其他人提出的相当多的问题,并且无法找到解决此问题的方法。
我正在创建的程序是Java中的Web爬虫程序,它使用Jsoup和MySQL。我目前正在Windows上进行Sublime编程。 (Eclipse遇到问题,厌倦了与之抗争)
当我尝试在爬虫程序中建立连接时程序正在破碎,但是在我的测试程序中,相同的过程在基本相同的过程中正常工作。
以下是相关代码的片段:
public void openConnection() throws SQLException, IOException
{
System.out.print("Opening connection...\n");
String drivers = props.getProperty("jdbc.drivers");
if (drivers != null) System.setProperty("jdbc.drivers", drivers);
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
maxUrls = Integer.parseInt(props.getProperty("crawler.maxurls"));
System.out.print("Connection line...\n");
//connection is a global
connection = DriverManager.getConnection(url, username, password);
}
属性为:
database.properties
jdbc.url=jdbc:mysql://localhost:3306/webdata
jdbc.username=root
jdbc.password=passwordofsomedescription
crawler.maxurls=100
crawler.domain=somewebsite.com
crawler.root=http://www.somewebsite.com
这个和测试程序的运行命令如下:
RunCrawler.bat
java.exe -classpath ".;./mysql-connector-java-5.1.29-bin.jar;./jsoup-1.7.3.jar" Crawler
RunTestDB.bat
java.exe -cp ".;./mysql-connector-java-5.1.29-bin.jar" TestDB
任何帮助将不胜感激。 Crawler需要同时使用mySQL和Jsoup。 testDB没有。
此外,两个程序编译正常。编译类路径没有错误(如下所示)
makeCrawler.bat
javac -classpath "./mysql-connector-java-5.1.29-bin.jar;./jsoup-1.7.3.jar" Crawler.java
答案 0 :(得分:0)
您正在从文件中读取驱动程序属性,但似乎没有定义。
并且您没有指示DriverManager
使用特定的Driver
来连接数据库。
建议1 :
将属性文件更改为包含驱动程序属性,如下所示。
database.properties
jdbc.url=jdbc:mysql://localhost:3306/webdata
jdbc.username=root
jdbc.password=passwordofsomedescription
crawler.maxurls=100
crawler.domain=somewebsite.com
crawler.root=http://www.somewebsite.com
# Add this line new
jdbc.drivers=com.mysql.jdbc.Driver
建议2 :
将drivers属性添加到文件后,
让JVM知道使用特定的驱动程序进行数据库连接。
Class.forName( drivers );
注意 :如果您使用JDBC 4.0
或更高版本,则此调用是可选的。