카테고리 없음

[DB] MySQL과 Java를 연동하여 간단히 테스트 하는 방법 (MySQLConn)

나스닥171819 2018. 11. 28. 17:48
728x90
반응형

MySQL과 Java를 연동하여 간단히 테스트 하는 방법 (MySQLConn)



https://sarc.io/index.php/mariadb/269-mysql-java-mysqlconn






Mariadb와 Java를 연동하여 간단히 테스트 하는 방법 (MySQLConn)

은 아래와 같다.


package db;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

public class MySQLConn {

    public static void main(String[] args) {

        Connection con = null;

        PreparedStatement pstmt = null;   

        ResultSet rs = null;

 

        try {

            Class.forName("org.mariadb.jdbc.Driver");

            

            con = DriverManager.getConnection(

                "jdbc:mariadb://127.0.0.1:3307/mysql",

                "root",

                "asdfasdf");

                        

            pstmt = con.prepareStatement("select * from idol"); //his_bus_voltage

            

            rs = pstmt.executeQuery();

            

            while(rs.next()) {

                //.

            int i;

            i = 0;

            String groupName = rs.getString("groupName");

String memberName = rs.getString("memberName");


System.out.print("\n** Group : " + groupName);

System.out.print("\n    -> Member: " + memberName);

            }

        } catch(Exception e) {

            e.printStackTrace();

        } finally {

            try {

                if(rs != null) {

                    rs.close(); // 선택 사항

                }

                

                if(pstmt != null) {

                    pstmt.close(); // 선택사항이지만 호출 추천

                }

            

                if(con != null) {

                    con.close(); // 필수 사항

                }

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

}


반응형