Getting error while connecting mysql database in java
I have created a program, i need to save the content into mysql database. Could you please help me how to save it through java programming
I have set the sqljdbc.jar classpath, but its giving errorI have used the following code
import java.sql.*;
public class connectURL {
p开发者_如何学运维ublic 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) {}
}
}
}
ERROR:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
- Your connection string
jdbc:sqlserver://127.0.0.1:8888;" + "databaseName=norton;user=root;password=
doesn't look like a valid connection string for a mysql database. Check this. - Aslo make sure you have mysql java connector in the build path.
Did you set Java Build Path to contain the sql jar?
精彩评论