hello.java
JDBC 연결 이있는 java 파일 이 있는데 실행하는 동안 오류 ( Ubuntu 18.04 LTS )가 발생합니다.
javac hello.java
java hello
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at hello.main(hello.java:22)
Goodbye!
그리고 클래스 경로로 시도하면 다음이 제공됩니다.
javac -cp /usr/share/java/mysql-connector-java-8.0.23.jar hello.java
java -cp /usr/share/java/mysql-connector-java-8.0.23.jar hello
Error: Could not find or load main class hello
mysql-connector-java-5.1.45.jar 도 시도했지만 여전히 동일한 문제입니다.
그리고 java -version
제공합니다 :
openjdk version "1.8.0_282"
OpenJDK Runtime Environment (build 1.8.0_282-8u282-b08-0ubuntu1~18.04-b08)
OpenJDK 64-Bit Server VM (build 25.282-b08, mixed mode)
다음 질문을 시도했습니다.
그러나 만족스러운 결과를 얻지 못했습니다. 누구든지 이것을 고치는 방법을 알고 있습니까?
내 코드는 다음과 같습니다 hello.java
.
//STEP 1. Import required packages
import java.sql.*;
public class hello {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/data";
// Database credentials
static final String USER = "newuser";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT * from temp";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
//Display values System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
//Handle errors for JDBC
e.printStackTrace();
} finally{
//finally block used to close resources
try{
if(stmt!=null) {
stmt.close();
}
}catch(SQLException se){
// nothing we can do
se.printStackTrace();
}
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
System.out.println("Goodbye!");
}//end main
}//end FirstExample
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다