목차
아래 파일을 다운받는다.
mariadb-java-client-3.1.3.jar
0.61MB
mariadb-java-client-3.1.3 : MariaDB 데이터베이스와 Java 애플리케이션을 연결하기 위한 JDBC 드라이버
eclipse에서 java project 생성 후
마우스 오른쪽 버튼 클릭 후에 Build Path > Configure Build Path

Libraries란에서 Classpath 클릭 후 Add External JARs 클릭

방금 다운받은 mariadb-java-client-3.1.3.jar 클릭

실행
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectionEx05 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("시작");
// 데이터베이스 연결
String url = "jdbc:mariadb://localhost:3306/sample";
String user = "root";
String password = "123456";
Connection conn = null;
Statement stmt = null;
try {
Class.forName("org.mariadb.jdbc.Driver");
System.out.println("드라이버 로딩 성공");
conn = DriverManager.getConnection(url, user, password);
System.out.println("데이터베이스 연결 성공");
stmt = conn.createStatement();
String deptno = "52";
String dname = " 연구부";
String loc = "대전";
// String sql = "insert into dept2 values (" + deptno + ", '" + dname + "','" + loc + "' )";
String sql = String.format("insert into dept2 values (%s, '%s', '%s')", deptno, dname, loc);
// System.out.println(sql);
stmt.executeQuery(sql);
System.out.println("입력 완료");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("[에러] " + e.getMessage());
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("[에러] " + e.getMessage());
} finally {
if (stmt != null) try {stmt.close();} catch (SQLException e) {}
if (conn != null) try {conn.close();} catch (SQLException e) {}
}
System.out.println("끝");
}
}
출력
시작
드라이버 로딩 성공
데이터베이스 연결 성공
입력 완료
끝
sql결과는 mariaDB command prompt에서 mysql -u root -p 로 접속해 확인 가능

'[Database DB] 데이터베이스' 카테고리의 다른 글
[데이터베이스/Database] mariadb SQL 실습 (grant, revoke, mysqldump) (0) | 2024.05.22 |
---|
아래 파일을 다운받는다.
mariadb-java-client-3.1.3.jar
0.61MB
mariadb-java-client-3.1.3 : MariaDB 데이터베이스와 Java 애플리케이션을 연결하기 위한 JDBC 드라이버
eclipse에서 java project 생성 후
마우스 오른쪽 버튼 클릭 후에 Build Path > Configure Build Path

Libraries란에서 Classpath 클릭 후 Add External JARs 클릭

방금 다운받은 mariadb-java-client-3.1.3.jar 클릭

실행
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class ConnectionEx05 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("시작"); // 데이터베이스 연결 String url = "jdbc:mariadb://localhost:3306/sample"; String user = "root"; String password = "123456"; Connection conn = null; Statement stmt = null; try { Class.forName("org.mariadb.jdbc.Driver"); System.out.println("드라이버 로딩 성공"); conn = DriverManager.getConnection(url, user, password); System.out.println("데이터베이스 연결 성공"); stmt = conn.createStatement(); String deptno = "52"; String dname = " 연구부"; String loc = "대전"; // String sql = "insert into dept2 values (" + deptno + ", '" + dname + "','" + loc + "' )"; String sql = String.format("insert into dept2 values (%s, '%s', '%s')", deptno, dname, loc); // System.out.println(sql); stmt.executeQuery(sql); System.out.println("입력 완료"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block System.out.println("[에러] " + e.getMessage()); } catch (SQLException e) { // TODO Auto-generated catch block System.out.println("[에러] " + e.getMessage()); } finally { if (stmt != null) try {stmt.close();} catch (SQLException e) {} if (conn != null) try {conn.close();} catch (SQLException e) {} } System.out.println("끝"); } }
출력
시작 드라이버 로딩 성공 데이터베이스 연결 성공 입력 완료 끝
sql결과는 mariaDB command prompt에서 mysql -u root -p 로 접속해 확인 가능

'[Database DB] 데이터베이스' 카테고리의 다른 글
[데이터베이스/Database] mariadb SQL 실습 (grant, revoke, mysqldump) (0) | 2024.05.22 |
---|