小编典典

如何将Java连接到Mysql?

java

我的Java程序出现这些错误。我已经把mysql-connector-java-5.1.14-bin.jar我的类路径放在里面了。如何解决呢?

HSystemRDB.java:144: package com.mysql.jdbc does not exist
    Driver driver = new com.mysql.jdbc.Driver();
                                      ^
HTestClassRDB.java:99: package com.mysql.jdbc does not exist
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());

代码:

    String url = "jdbc:mysql://wire:3306/h?user="+pSystemRDB.USERNAME+"&password="+pSystemRDB.PASSWORD;
    Connection con;
    Statement stmt;
    String query1 = "Delete from dbase";
    String query2 = "Delete from id";


    try {
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
    } catch (Exception e) {
        System.out.println("Class Not Found Exception:");
        System.out.println(e.getMessage());
    }

阅读 218

收藏
2020-10-16

共1个答案

小编典典

您需要从下载MySQL包:这里并将其放置在库中,我将修改在少数分钟excact步骤

这是连接数据库的正确语法:

try
{
  // create a java mysql database connection
  String myDriver = "org.gjt.mm.mysql.Driver";
  String myUrl = "jdbc:mysql://localhost/test";
  Class.forName(myDriver);
  Connection conn = DriverManager.getConnection(myUrl, "root", "");

  // your prepstatements goes here...

  conn.close();
}
catch (Exception e)
{
  System.err.println("Got an exception! ");
  System.err.println(e.getMessage());
}

希望这可以帮助

2020-10-16