관리 메뉴

moozi

jdbc mysql 기초 본문

TIS_2017/응용sw_1기

jdbc mysql 기초

moozi 2016. 12. 30. 15:30

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;


public class JDBCMySQL {


public static void main(String[] args) {

Connection conn = null; // 연결객체

        ResultSet rs = null;    // select한 결과를 저장하는 객체

        Statement stmt = null;  // sql을 실행하기 위한 객체 

        String url = null;      // 서버 url

        String id = "root";       // ID

        String pw = "1234";     // PW

        

        url ="jdbc:mysql://localhost:3306/sampledb?autoReconnect=true&useSSL=false";

        try {

Class.forName("com.mysql.jdbc.Driver");// jdbc driver load

conn = DriverManager.getConnection(url,id,pw);// 연결

System.out.println("연결되었습니다.");

        

        

stmt = conn.createStatement(); // stament객체 생성

rs = stmt.executeQuery("select * from test");  //쿼리실행

       

while(rs.next()){ // 한행씩 레코드를 읽어옴. Fetch. 데이터가 있으면 true.

   String sid = rs.getString(1); // 컬럼index로 데이터를 읽어옴.1번부터

   

   System.out.println(sid);

}


       

stmt.close();

conn.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}


}



'TIS_2017 > 응용sw_1기' 카테고리의 다른 글

web.zip 01/04  (0) 2017.01.04
html연습문제01  (0) 2017.01.03
java연습문제07풀이  (0) 2016.12.29
jdbc연습문제03-미니프로젝트  (0) 2016.12.28
jdbc연습문제02  (0) 2016.12.27
Comments