java.sql.SQLException:jdbc:mysql://localhost:3306/dbname에 적합한 드라이버를 찾을 수 없습니다.
다음 Java 프로그램이 있습니다.MySQLConnectExample.java
import java.sql.*;
import java.util.Properties;
public class MySQLConnectExample {
public static void main(String[] args) {
Connection conn1 = null;
Connection conn2 = null;
Connection conn3 = null;
try {
String url1 = "jdbc:mysql://localhost:3306/aavikme";
String user = "root";
String password = "aa";
conn1 = DriverManager.getConnection(url1, user, password);
if (conn1 != null)
System.out.println("Connected to the database test1");
String url2 = "jdbc:mysql://localhost:3306/aavikme?user=root&password=aa";
conn2 = DriverManager.getConnection(url2);
if (conn2 != null) {
System.out.println("Connected to the database test2");
}
String url3 = "jdbc:mysql://localhost:3306/aavikme";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "aa");
conn3 = DriverManager.getConnection(url3, info);
if (conn3 != null) {
System.out.println("Connected to the database test3");
}
} catch (SQLException ex) {
System.out.println("An error occurred. Maybe user/password is invalid");
ex.printStackTrace();
}
}
}
다음과 같이 컴파일합니다.
E:\java mysql code driver>javac MySQLConnectExample.java
E:\java mysql code driver>java -cp mysql-connector-java-3.0.11-stable-bin.jar;.
MySQLConnectExample
다음의 에러가 표시됩니다.
An error occurred. Maybe user/password is invalid
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/
aavikme
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at MySQLConnectExample.main(MySQLConnectExample.java:20)
내가 뭘 잘못하고 있지?
먼저 다음을 실행하십시오.
Class.forName("com.mysql.jdbc.Driver");
이것에 의해, Java가 이러한 데이타베이스 접속 스트링을 처리하는 방법을 알 수 있도록, 드라이버가 자동적으로 등록됩니다.
자세한 내용은 MySQL Connector 참조를 참조하십시오.
로딩해야 합니다.jdbc driver
아래 강령을 고려하십시오.
try {
Class.forName("com.mysql.jdbc.Driver");
// connect way #1
String url1 = "jdbc:mysql://localhost:3306/aavikme";
String user = "root";
String password = "aa";
conn1 = DriverManager.getConnection(url1, user, password);
if (conn1 != null) {
System.out.println("Connected to the database test1");
}
// connect way #2
String url2 = "jdbc:mysql://localhost:3306/aavikme?user=root&password=aa";
conn2 = DriverManager.getConnection(url2);
if (conn2 != null) {
System.out.println("Connected to the database test2");
}
// connect way #3
String url3 = "jdbc:mysql://localhost:3306/aavikme";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "aa");
conn3 = DriverManager.getConnection(url3, info);
if (conn3 != null) {
System.out.println("Connected to the database test3");
}
} catch (SQLException ex) {
System.out.println("An error occurred. Maybe user/password is invalid");
ex.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
같은 문제가 있었습니다.코드는 다음과 같습니다.
private Connection conn = DriverManager.getConnection(Constant.MYSQL_URL, Constant.MYSQL_USER, Constant.MYSQL_PASSWORD);
private Statement stmt = conn.createStatement();
드라이버 클래스는 로드하지 않았지만 로컬로 동작합니다.MySQL의 결과를 쿼리할 수 있지만 Tomcat에 전개하면 동작하지 않고 다음 오류가 발생합니다.
No suitable driver found for jdbc:mysql://172.16.41.54:3306/eduCloud
그래서 다른 답변이 게시되어 있는 것을 보고 아래와 같이 드라이버 클래스를 로드했습니다.
Class.forName("com.mysql.jdbc.Driver");
이제 됐다!왜 현지에서 잘 돌아가는지 모르겠어요. 당신의 도움이 필요해요. 정말 고마워요!
컬럼 1, 컬럼2, 컬럼3 컬럼4, cloumn1 및 2 홀드 int 값과 컬럼3 및 4 홀드가 있는 테이블에서 데이터를 검색하는 예varchar(10)
import java.sql.*;
// need to import this as the STEP 1. Has the classes that you mentioned
public class JDBCexample {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://LocalHost:3306/databaseNameHere";
// DON'T PUT ANY SPACES IN BETWEEN and give the name of the database (case insensitive)
// database credentials
static final String USER = "root";
// usually when you install MySQL, it logs in as root
static final String PASS = "";
// and the default password is blank
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// registering the driver__STEP 2
Class.forName("com.mysql.jdbc.Driver");
// returns a Class object of com.mysql.jdbc.Driver
// (forName(""); initializes the class passed to it as String) i.e initializing the
// "suitable" driver
System.out.println("connecting to the database");
// opening a connection__STEP 3
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// executing a query__STEP 4
System.out.println("creating a statement..");
stmt = conn.createStatement();
// creating an object to create statements in SQL
String sql;
sql = "SELECT column1, cloumn2, column3, column4 from jdbcTest;";
// this is what you would have typed in CLI for MySQL
ResultSet rs = stmt.executeQuery(sql);
// executing the query__STEP 5 (and retrieving the results in an object of ResultSet)
// extracting data from result set
while(rs.next()){
// retrieve by column name
int value1 = rs.getInt("column1");
int value2 = rs.getInt("column2");
String value3 = rs.getString("column3");
String value4 = rs.getString("columnm4");
// displaying values:
System.out.println("column1 "+ value1);
System.out.println("column2 "+ value2);
System.out.println("column3 "+ value3);
System.out.println("column4 "+ value4);
}
// cleaning up__STEP 6
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// handle sql exception
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception for class.forName
e.printStackTrace();
}finally{
//closing the resources..STEP 7
try {
if (stmt != null)
stmt.close();
} catch (SQLException e2) {
e2.printStackTrace();
}try {
if (conn != null) {
conn.close();
}
} catch (SQLException e2) {
e2.printStackTrace();
}
}
System.out.println("good bye");
}
}
복사하지 않았을 수 있습니다.MySQL connector/J
jar 파일을 lib 폴더에 넣은 다음 이 파일이 클래스 경로에 있어야 합니다.
만약 그렇게 하지 않았다면, 제게 알려주세요. 제가 답을 자세히 설명하겠습니다.
코드에 누락되어 있습니다.Class.forName("com.mysql.jdbc.Driver");
이것이 당신이 모든 것을 작동시키기 위해 놓치고 있는 것입니다.
여기의 모든 답변은Class.forName("my.vandor.Driver");
드라이버를 로드하기 위한 라인.
(더 나은) 대안으로DriverManager
도우미 클래스에는 JDBC 드라이버를 처리하는 몇 가지 방법이 있습니다.
하고 싶을지도 몰라
- 사용하다
DriverManager.registerDriver(driverObject);
드라이버를 드라이버 목록에 등록하다
지정된 드라이버를 Driver Manager에 등록합니다.새로 로드된 드라이버 클래스는 메서드 registerDriver를 호출하여 DriverManager에게 알려야 합니다.드라이버가 현재 등록되어 있는 경우는, 아무것도 행해지지 않습니다.
- 사용하다
DriverManager.deregisterDriver(driverObject);
제거할 수 있습니다.
지정된 드라이버를 DriverManager의 등록된 드라이버 목록에서 제거합니다.
예:
Driver driver = new oracle.jdbc.OracleDriver();
DriverManager.registerDriver(driver);
Connection conn = DriverManager.getConnection(url, user, password);
// ...
// and when you don't need anything else from the driver
DriverManager.deregisterDriver(driver);
또는 DataSource를 사용하는 것이 좋습니다.
이거 먹어봐
String url = "jdbc:mysql://localhost:3306/<dbname>";
String user = "<username>";
String password = "<password>";
conn = DriverManager.getConnection(url, user, password);
저도 같은 문제가 있었습니다.Mysql 서버가 실행되고 있는 포트를 확인하면 문제가 해결됩니다.
예를 들어, 제 코드는 다음과 같습니다.
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:8080/bddventas","root","");
스트링을 로 변경합니다.
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bddventas","root","");
그리고 voila!! 이 동작은 서버가 그 포트에서 실행되고 있었기 때문에 가능했습니다.
도움이 되었으면 좋겠다
언급URL : https://stackoverflow.com/questions/22384710/java-sql-sqlexception-no-suitable-driver-found-for-jdbcmysql-localhost3306
'programing' 카테고리의 다른 글
Gson이 value=map 엔트리를 무시합니다. (0) | 2022.10.18 |
---|---|
php 날짜를 mysql 형식으로 변환 (0) | 2022.10.18 |
여러 요청을 사용하여 액세스 토큰 새로 고침 (0) | 2022.10.18 |
j회피해야 할 함정 쿼리 (0) | 2022.10.18 |
PHP 어플리케이션에서 MySQL 대신 Redis를 사용해야 할 때는 언제입니까? (0) | 2022.10.18 |