我已经创建了一个程序,我需要将内容保存到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
最佳答案
您的连接字符串jdbc:sqlserver://127.0.0.1:8888;" + "databaseName=norton;user=root;password=
看起来不像mysql数据库的有效连接字符串。检查一下。
Aslo确保在构建路径中有mysql java connector。
关于java - 在Java中连接mysql数据库时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6808388/