기타 기술/Java
JDBC 코드(Maven)
hawon6691
2025. 3. 31. 00:49
728x90
package org.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionTest {
public static void main(String[] args) {
Connection conn = null;
try {
conn =
DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/samplesdb?useUnicode=true&serverTimezone=Asia/Seoul",
"rootoj",
"1234");
if(conn != null) {
System.out.println("DBMS 연결 성공!!");
System.out.println(conn.getClass().getName());
}
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} finally {
try {
if(conn != null) conn.close();
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
}
}
}
728x90