【发布时间】:2011-07-24 17:19:47
【问题描述】:
我已经创建了一个程序,我需要将内容保存到 mysql 数据库中。你能帮我如何通过java编程来保存它
我已经设置了 sqljdbc.jar 类路径,但是它给出了错误
我使用了以下代码
import java.sql.*;
public class connectURL {
public static void main(String[] args) {
String connectionUrl = "jdbc:sqlserver://127.0.0.1:8888;" +
"databaseName=norton;user=root;password=";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(connectionUrl);
String SQL = "SELECT TOP 10 * FROM demopoll";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
while (rs.next()) {
System.out.println(rs.getString(4) + " " + rs.getString(6));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
}
}
错误:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
【问题讨论】:
-
您已将其标记为 MySQL,jdbc 字符串用于此。但是 JDBC 错误是针对 SQL Server 的——Microsoft 产品...
-
您在这里使用的是 SQL Server 还是 MySQL?您的问题被标记为 mysql,而您使用的 JDBC 驱动程序类 (
com.mysql.jdbc.Driver) 用于 MySQL,但 sqljdbc.jar 是您用于连接 SQL Server 的 JAR,连接字符串还建议使用 SQL Server,而您的 ClassNotFoundException (这似乎与您的代码不匹配)提到了 SQL Server 驱动程序。此外,SQL (SELECT TOP n ...) 在 SQL Server 中有效,但在 MySQL 中无效。