我正在尝试编写代码,使用 Eclipse、MySQL Workbench 和 JDBC 8.0.11 将文本文件的数据导入数据库。它给了我一个 ClassNotFoundException。我已经查看了多个其他问题,并且通过将 java\com\mysql\jdbc\Driver.java 放入 DriverManager.getConnection 参数中来解决所有问题。我已经这样做了,但它仍然给我一个错误。有人知道为什么我仍然收到此错误吗?
public static void main(String[] args) throws Exception{
Class.forName //Register JDBC Driver
("*mysql-connector-java-8.0.11.\\src\\legacy\\java\\com\\mysql\\jdbc\\Driver.java*")
.newInstance();
conn = DriverManager.getConnection (url, user, pass);
Statement stmt = conn.createStatement();
String mysql1 = "UPDATE Policy SET " + readAndArray //Changeable file path
("filepath");
}
NEW EDIT
按照 @zlakad 的建议,如果您有 Java 6 或更高版本,则不需要使用 Class.forName() 。虽然,现在我有一个新错误:SQLNonTransientConnectionException,因为底层的 WrongArgumentException。我很困惑为什么它会这样做,因为我没有为 DriverManager.getConnection 使用不正确的参数。有什么建议么?
String url = "file path"; //Changeable for MySQL DB
String user = "root";
String pass = "password";
public static void getConnection() throws Exception {
Connection conn = DriverManager.getConnection(url, user, pass);
Statement stmt = conn.createStatement();
尝试这个:
// None of this belongs in a main method.
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
// where are url, user, pass set? I don't see them.
Connection conn = DriverManager.getConnection(url, user, pass);
Statement stmt = conn.createStatement();
// this is simply wrong.
String mysql1 = "UPDATE Policy SET " + readAndArray("filepath");
}
您是 Java 和 JDBC 新手。这不是一个好方法。我建议您在网络上搜索一些如何做得更好的示例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)