0

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 error

I have used the following code

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) {}
    }
  }
}

ERROR:

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

2
  • You've tagged this as MySQL, which the jdbc string is for. But the JDBC error is for SQL Server -- a Microsoft product... Commented Jul 24, 2011 at 17:41
  • Are you using SQL Server or MySQL here? Your question is tagged mysql, and the JDBC driver class (com.mysql.jdbc.Driver) you're using is for MySQL, but sqljdbc.jar is the JAR you use to connect to SQL Server, the connection string also suggests SQL Server, and your ClassNotFoundException (which appears not to match your code) mentions the SQL Server driver. Furthermore, the SQL (SELECT TOP n ...) is valid in SQL Server but not in MySQL. Commented Jul 24, 2011 at 20:59

2 Answers 2

2
  1. 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.
  2. Aslo make sure you have mysql java connector in the build path.
Sign up to request clarification or add additional context in comments.

Comments

0

Did you set Java Build Path to contain the sql jar?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.