JDBC

MySQL JDBC 테스트

import java.sql.*;
 
public class TestMySQLJDBC {
    public static void main(String[] args) {
        Connection conn = null;
        Statement  stmt = null;
        ResultSet  rs   = null;
 
        System.out.println("Test MySQL JDBC...");
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception ex) {
            System.out.println("Not Found Driver...");
            return;
        }
 
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost/디비이름?user=사용자아이디&password=비밀번호&characterEncoding=UTF-8&useUnicode=yes");
            stmt = conn.createStatement();
            /*
            stmt.executeQuery("set names utf8");
            stmt.executeQuery("set character_set_client = utf8");
            stmt.executeQuery("set character_set_results = utf8");
            stmt.executeQuery("set character_set_connection = utf8");
            */
            rs   = stmt.executeQuery("select code_class_id, code_class_name from tb_code_class");
            while(rs.next()) {
                System.out.print("Code Class ID: " + rs.getString("code_class_id"));
                System.out.println(", Code Class Name: " + rs.getString("code_class_name"));
            }
            rs.close();
            stmt.close();
        } catch(SQLException ex) {
            if(rs != null)      rs = null;
            if(stmt != null)    stmt = null;
        }
 
        System.out.println("\nEnd...");
    }
}