MySQL JDBC

mysql jdbc logo에 대한 이미지 검색결과

JDBC(Java Database Connectivity)는 자바에서 데이터베이스에 접속할 수 있도록 하는 자바 API이다. JDBC는 데이터베이스에서 자료를 쿼리하거나 업데이트하는 방법을 제공한다.


MySQL JDBC 설치방법


먼저 MySQL을 설치합니다.  (링크 MySQL)

$ sudo apt-get install libmysql-java 를 입력합니다.


데이터베이스, 유저 생성

$ mysql -u root -p

mysql 비밀번호를 입력합니다.


mysql> create database test_db;

mysql> grant all on test_db.* to test_user@localhost identified by “test_pw”;

quit


vi ~/.bashrc

제일 마지막 줄로 갑니다.

CLASSPATH=$CLASSPATH:/usr/share/java/mysql.jar

export CLASSPATH

를 입력하고 저장합니다.

source ~/.bashrc

echo $CLASSPATH 를 입력하여 확인합니다.


MySQL 연결


vi JDBCTest.java

아래 코드를 붙여넣기 합니다.


import java.sql.*;

import java.util.Properties;


public class JDBCTest

{

 // The JDBC Connector Class.

 private static final String dbClassName = "com.mysql.jdbc.Driver";


 // Connection string. test_db is the database the program

 // is connecting to. You can include user and password after this


 private static final String CONNECTION =

                         "jdbc:mysql://127.0.0.1/test_db?useSSL=false";

 private static final String USER = "test_user";

 private static final String PASS = "test_pw";


 public static void main(String[] args) throws

                            ClassNotFoundException,SQLException

 {

   System.out.println(dbClassName);

   // Class.forName(xxx) loads the jdbc classes and

   // creates a drivermanager class factory

   Class.forName(dbClassName);


   // Now try to connect

   Connection c = DriverManager.getConnection(CONNECTION, USER, PASS);


   System.out.println("It works !");

   c.close();

   }

}



sudo javac JDBCTest.java


java JDBCTest


테이블 생성


vi MakeTable.java


아래 코드를 붙여넣기 합니다.


import java.sql.*;

import java.util.Properties;


public class MakeTable

{

 // The JDBC Connector Class.

 private static final String dbClassName = "com.mysql.jdbc.Driver";


 // Connection string. test_db is the database the program

 // is connecting to. You can include user and password after this


 private static final String CONNECTION =

                         "jdbc:mysql://127.0.0.1/test_db?useSSL=false";

 private static final String USER = "test_user";

 private static final String PASS = "test_pw";


 public static void main(String[] args) throws

                            ClassNotFoundException,SQLException

 {

   System.out.println(dbClassName);

   // Class.forName(xxx) loads the jdbc classes and

   // creates a drivermanager class factory

   Class.forName(dbClassName);


   // Now try to connect

   Connection con = DriverManager.getConnection(CONNECTION, USER, PASS);

   Statement stmt = con.createStatement ();

   String sql ="CREATE TABLE test_table (" +

               "id INTEGER not NULL, " +

               "name VARCHAR(20), " +

               "email VARCHAR(20), " +

               "PRIMARY KEY ( id ))";

   stmt.executeUpdate(sql);

   System.out.println("It works !");

   stmt.close();

   con.close();

   }

}



sudo javac MakeTable.java

mysql -u test_user -p test_db

test_pw

SELECT * FROM test_table;

Empty set인 것을 확인할 수 있습니다.



데이터 삽입

vi InsertData.java

아래 코드를 입력한다.

import java.sql.*;

import java.util.Properties;


public class InsertData

{

 // The JDBC Connector Class.

 private static final String dbClassName = "com.mysql.jdbc.Driver";


 // Connection string. test_db is the database the program

 // is connecting to. You can include user and password after this


 private static final String CONNECTION =

                         "jdbc:mysql://127.0.0.1/test_db?useSSL=false";

 private static final String USER = "test_user";

 private static final String PASS = "test_pw";


 public static void main(String[] args) throws

                            ClassNotFoundException,SQLException

 {

   System.out.println(dbClassName);

   // Class.forName(xxx) loads the jdbc classes and

   // creates a drivermanager class factory

   Class.forName(dbClassName);


   // Now try to connect

   Connection con = DriverManager.getConnection(CONNECTION, USER, PASS);

   Statement stmt = con.createStatement ();

   String sql ="INSERT INTO test_table " +

               "VALUES (1, 'A', 'A@kookmin.ac.kr')";

   stmt.executeUpdate(sql);

   sql = "INSERT INTO test_table " +

               "VALUES (2, 'B', 'B@kookmin.ac.kr')";

   stmt.executeUpdate(sql);

   sql = "INSERT INTO test_table " +

               "VALUES (3, 'C', 'C@kookmin.ac.kr')";

   stmt.executeUpdate(sql);

   System.out.println("It works !");

   stmt.close();

   con.close();

   }

}




sudo javac InsertData.java

java InsertData

mysql -u -test_user -p test_db

test_pw

SELECT * FROM test_table;



참조 사이트

https://www.tutorialspoint.com/jdbc/index.htm




Comments