[문제 1] 아래 <조건>을 보고 'sw3_Setting.jar'을 작성하시오.
<조건>
1) sw3_Setting.jar 실행하면 DB ‘sw3_선수번호’가 생성되도록 하시오
(기존에 만들어진 DB 및 테이블은 모두 삭제되도록 하시오.)
2) DB에 아래와 같은 테이블이 생성되고, 제공되는 텍스트 파일이 각 테이블로 입력될 수 있도록 하시
오.
3) DB 사용자 아이디를 ‘user’, 패스워드를 ‘1234’로 생성하시오.
4) 이 사용자는 ‘sw3_선수번호’의 데이터 조회/추가/삭제/업데이트만 가능하도록 하시오.
이외 테이블 생성이나 user 관리 등의 권한은 제한되도록 하시오.
이후 과제의 모든 Connection은 ‘user’로 접속할 수 있도록 하시오.
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.StringTokenizer; public class Initialize { static Connection conn = null; static String sql = ""; static String db = "jdbc:mysql://127.0.0.1/"; static Statement stmt = null; String option = "?useSSL=false&characterEncoding=utf8&user=root&password=5071"; public Initialize() { try{ System.out.println("connecting..."); Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(db+option); System.out.println("connect complete!"); }catch(ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public static void existsDB () { if(sql!="") sql=""; try { stmt = conn.createStatement(); sql = "DROP DATABASE IF EXISTS `sw3_5071`"; stmt.executeUpdate(sql); sql = "CREATE DATABASE `sw3_5071`"; stmt.executeUpdate(sql); db = "jdbc:mysql://127.0.0.1/sw3_5071"; } catch(SQLException e) { e.printStackTrace(); } } public static void createTable () { if(sql!="") sql=""; try { stmt = conn.createStatement(); //TBL_Customer sql = "CREATE TABLE `TBL_Customer` (" + "`cID` varchar(6) NOT NULL," + "`cPW` varchar(4)," + "`cName` varchar(10)," + "`cHP` varchar(13)," + "PRIMARY KEY(cID));"; stmt.executeUpdate(sql); //TBL_Bus sql = "CREATE TABLE `TBL_Bus` (" + "`bNumber` varchar(4) NOT NULL," + "`bDeparture` varchar(5)," + "`bArrival` varchar(5)," + "`bTime` time," + "`bElapse` varchar(10)," + "`bCount` varchar(1)," + "`bPrice` int(6)," + "PRIMARY KEY(bNumber));"; stmt.executeUpdate(sql); //TBL_Ticket sql = "CREATE TABLE `TBL_Ticket` (" + "`bDate` date," + "`bNumber` varchar(4)," + "`bNumber2` varchar(5)," + "`bSeat` int(2)," + "`cID` varchar(6)," + "`bPrice` int(6)," + "`bState` varchar(1));"; stmt.executeUpdate(sql); }catch(SQLException e) { e.printStackTrace(); } } public static void insertDate (String table, int column_cnt) { if(sql!="") sql = ""; try { FileInputStream fi = new FileInputStream("C://Users/김벼ㅇ머/Downloads/20171490141449168A/정보기술/정보기술(제2과제)/"+table+".txt"); InputStreamReader is = new InputStreamReader(fi,"UTF-8"); BufferedReader br = new BufferedReader(is); StringTokenizer st = null; String readRow = ""; int columnHead = 1; stmt = conn.createStatement(); while ((readRow = br.readLine())!= null) { st = new StringTokenizer(readRow," "); String[] arr = new String[column_cnt]; int i = 0; while(st.hasMoreTokens()) { arr[i++] = st.nextToken(); } if(columnHead != 1) { if(table == "TBL_BUS") { sql = "INSERT INTO sw3_5071.TBL_Bus VALUES(" + "'"+arr[0]+"','"+arr[1]+"','"+arr[2]+"','"+arr[3]+"','"+arr[4]+"','"+arr[5]+"','"+arr[6]+"');"; }else if(table == "TBL_CUSTOMER") { sql = "INSERT INTO sw3_5071.TBL_Customer VALUES(" + "'"+arr[0]+"','"+arr[1]+"','"+arr[2]+"','"+arr[3]+"');"; }else if(table == "TBL_TICKET") { sql = "INSERT INTO sw3_5071.TBL_Ticket VALUES(" + "'"+arr[0]+"','"+arr[1]+"','"+arr[2]+"','"+arr[3]+"','"+arr[4]+"','"+arr[5]+"','"+arr[6]+"');"; } stmt.executeUpdate(sql); } columnHead++; } if(br!=null) br.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public static void createUser() { if(sql!="") sql =""; try { stmt = conn.createStatement(); sql = "DROP USER IF EXISTS user@127.0.0.1"; stmt.executeUpdate(sql); sql = "CREATE USER user@127.0.0.1"; stmt.executeUpdate(sql); sql = "GRANT SELECT,INSERT,DELETE,UPDATE ON sw3_5071.* TO user@127.0.0.1"; stmt.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } } public static void close() { try{if(conn!=null) conn.close();}catch(SQLException e) {e.printStackTrace();} try{if(stmt!=null) stmt.close();}catch(SQLException e) {e.printStackTrace();} System.out.println("Close Complete!"); } public static void main(String[] args) { new Initialize(); existsDB(); new Initialize(); createTable(); insertDate("TBL_BUS",7); insertDate("TBL_CUSTOMER",4); insertDate("TBL_TICKET",7); close(); } } | cs |
'옛날' 카테고리의 다른 글
HTML 소스코드 하이라이팅 (0) | 2017.08.17 |
---|---|
[C#] 형변환 (0) | 2017.04.13 |
[지방] 공개과제 문제1 완벽소스 (0) | 2017.03.21 |
[JAVA] 팁 모음 (0) | 2017.03.16 |
[엑셀VBA] VBA로 시트의 숫자를 한글로 바꾸는 방법 (0) | 2017.03.08 |