简体   繁体   English

在java中连接mysql数据库时出错

[英]Getting error while connecting mysql database in java

I have created a program, i need to save the content into mysql database. 我已经创建了一个程序,我需要将内容保存到mysql数据库中。 Could you please help me how to save it through java programming 你能帮我解决一下如何通过java编程保存它
I have set the sqljdbc.jar classpath, but its giving error 我已经设置了sqljdbc.jar类路径,但它给出了错误

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 java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver

  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. 您的连接字符串jdbc:sqlserver://127.0.0.1:8888;" + "databaseName=norton;user=root;password=看起来不像是mysql数据库的有效连接字符串。 Check this. 检查一下。
  2. Aslo make sure you have mysql java connector in the build path. Aslo确保你在构建路径中有mysql java 连接器

您是否将Java Build Path设置为包含sql jar?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM