728x90
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | package db; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class JDBC3 { // 파일내용읽어온 후 저장 public static void main(String[] args) { BufferedReader reader = null; Reader r = null; List<String> list = null; Connection con = null; PreparedStatement stmt =null; int count =0; try { r = new FileReader("data.txt"); reader = new BufferedReader(r); list = new ArrayList<String>(); // 자료 담을 자료구조 생성 while (true) { String data = reader.readLine(); if (data == null) break; String[] arr = data.split(","); count++;// 배열에서 값을 하나씩 꺼내어 insert하기 좋은 형태로 저장 // ex) map, list // 여기까지 ,를 구분으로 배열이 만들어졌는데 for (String string : arr) { // 하나씩 뽑아서 리스트에 add list.add(string); } System.out.println(list); } //디비연결 후 넣기 Class.forName("com.mysql.jdbc.Driver"); //그냥 외울것 String url = "jdbc:mysql://localhost:3306/java"; String id = "root"; String pw = "1234"; con = DriverManager.getConnection(url, id, pw); // Connection StringBuffer query = new StringBuffer(); // 스트링버퍼열기 쿼리에다가 담을거야 query.append("INSERT INTO Student VALUES (?,?,?,?)"); // 데이터를 조회하는거 (?,?) 플레이스홀더 , 프레임, 틀, 을 가지고 변경되는 내용만 그때그때 // 입력. stmt = con.prepareStatement(query.toString()); for (int i = 0; i < count; i++) { // 데이터 길이만큼 stmt.setInt(1, i + 1); stmt.setString(2, list.get((3 * i) + 0)); // 또넣으면 프라이머리키 중복되서 안되고. stmt.setString(3, list.get((3 * i) + 1)); stmt.setString(4, list.get((3 * i) + 2)); // insert, update, delete 값(데이터)에 변화를 주는. executeQuery()는 조회시/ stmt.executeUpdate(); } } catch (FileNotFoundException e) { e.printStackTrace(); } // 데이터 파일준비 catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (r!= null) //이렇게 하는 경우 r이 생성되는 단계에서 오류가 발생할 수 있다 try { r.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NullPointerException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { con.close(); } catch (SQLException e) { e.printStackTrace(); } catch (NullPointerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 파일을 읽어들이고 저장까지 // 그리고 저장까지 하고 데이터베이스에 담는건 다음 소스코드부터 } } //다른 방법들 //철진이방법: 큰배열, 작은배열 | cs |