代码中的错误:java.sql.SQLException:找不到适合TEST / ANKUR的驱动程序

时间:2022-11-22 22:53:00
package javaapplication1;
import java.sql.*;
public class JavaApplication1{
  public static void main(String[] args) {
  System.out.println("MySQL Connect Example.");
  Connection conn = null;
  String url = "jdbc:mysql://localhost:3306/TEST/ANKUR1";
  String dbName = "jdbctutorial";
  String driver = "com.mysql.jdbc.Driver";
  String userName = "root"; 
  String password = "root";
  try {
  Class.forName(driver).newInstance();
  conn = DriverManager.getConnection("TEST1/ANKUR","root","school");
  System.out.println("Connected to the database");
  conn.close();
  System.out.println("Disconnected from database");
  } catch (Exception e) {
      System.out.println(e);
  }
  }
}

I had installed MySQL 5.5 on my machine and I copied Java-connector.jar file in the required folders. My Database is test1 and table is ankur. Please see I was using this code to just to check the connectivity with my NetBeans 7. Any suggestions?

我在我的机器上安装了MySQL 5.5,并将Java-connector.jar文件复制到所需的文件夹中。我的数据库是test1,表格是ankur。请参阅我使用此代码来检查与NetBeans 7的连接。有什么建议吗?

2 个解决方案

#1


0  

The URL you actually use in getConnection(...) is wrong. You are not supposed to put in just the name of the table you want to work with. You need to have a complete URL so the JDBC system knows that it should use the mysql driver and where the mysql driver needs to connect to.

您在getConnection(...)中实际使用的URL是错误的。您不应该只输入要使用的表的名称。您需要有一个完整的URL,以便JDBC系统知道它应该使用mysql驱动程序以及mysql驱动程序需要连接的位置。

Recheck the example you copied from, and see where they use the "url" variable.

重新检查您复制的示例,并查看它们使用“url”变量的位置。

#2


0  

The JDBC URL format when using the MySQL Connector/J driver is:

使用MySQL Connector / J驱动程序时的JDBC URL格式为:

jdbc:mysql://[host][,failoverhost...][:port]/[database][?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

Therefore, the actual URL to be used in your case ought to be (if the database is installed on your local machine with name TEST and listening on the default MySQL port of 3306):

因此,您的案例中应使用的实际URL应该是(如果数据库安装在本地计算机上,名称为TEST并且侦听默认的MySQL端口3306):

jdbc:mysql://localhost:3306/TEST

and not the below.

而不是下面的。

TEST/ANKUR1

Remember that JDBC URLs are used by the DriverManager class to determine which driver ought to be used to connect to the database, and where the database is located. One does not connect to tables in JDBC; instead, one connects to a database (or a schema) and then issues SELECT queries against tables. Your method therefore, would look like:

请记住,DriverManager类使用JDBC URL来确定应该使用哪个驱动程序连接到数据库,以及数据库所在的位置。一个不连接到JDBC中的表;相反,一个连接到数据库(或模式),然后对表发出SELECT查询。因此,您的方法看起来像:

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/TEST","root","school");
System.out.println("Connected to the database");
PreparedStatement pStmt = conn.prepareStatement("SELECT * FROM ANKUR");
ResultSet rs = pStmt.executeQuery();
...
//Process the contents of the ResultSet
...
rs.close();
pStmt.close();
conn.close();

It should be noted that it a good practice to close the ResultSet, PreparedStatement and Connection objects in a finally block.

应该注意,最好在finally块中关闭ResultSet,PreparedStatement和Connection对象。

#1


0  

The URL you actually use in getConnection(...) is wrong. You are not supposed to put in just the name of the table you want to work with. You need to have a complete URL so the JDBC system knows that it should use the mysql driver and where the mysql driver needs to connect to.

您在getConnection(...)中实际使用的URL是错误的。您不应该只输入要使用的表的名称。您需要有一个完整的URL,以便JDBC系统知道它应该使用mysql驱动程序以及mysql驱动程序需要连接的位置。

Recheck the example you copied from, and see where they use the "url" variable.

重新检查您复制的示例,并查看它们使用“url”变量的位置。

#2


0  

The JDBC URL format when using the MySQL Connector/J driver is:

使用MySQL Connector / J驱动程序时的JDBC URL格式为:

jdbc:mysql://[host][,failoverhost...][:port]/[database][?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

Therefore, the actual URL to be used in your case ought to be (if the database is installed on your local machine with name TEST and listening on the default MySQL port of 3306):

因此,您的案例中应使用的实际URL应该是(如果数据库安装在本地计算机上,名称为TEST并且侦听默认的MySQL端口3306):

jdbc:mysql://localhost:3306/TEST

and not the below.

而不是下面的。

TEST/ANKUR1

Remember that JDBC URLs are used by the DriverManager class to determine which driver ought to be used to connect to the database, and where the database is located. One does not connect to tables in JDBC; instead, one connects to a database (or a schema) and then issues SELECT queries against tables. Your method therefore, would look like:

请记住,DriverManager类使用JDBC URL来确定应该使用哪个驱动程序连接到数据库,以及数据库所在的位置。一个不连接到JDBC中的表;相反,一个连接到数据库(或模式),然后对表发出SELECT查询。因此,您的方法看起来像:

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/TEST","root","school");
System.out.println("Connected to the database");
PreparedStatement pStmt = conn.prepareStatement("SELECT * FROM ANKUR");
ResultSet rs = pStmt.executeQuery();
...
//Process the contents of the ResultSet
...
rs.close();
pStmt.close();
conn.close();

It should be noted that it a good practice to close the ResultSet, PreparedStatement and Connection objects in a finally block.

应该注意,最好在finally块中关闭ResultSet,PreparedStatement和Connection对象。