Wed/Jan/15 [Java] day12. Java-SQL-HTML 연습문제2
Wed/Jan/15
[Java] day12.
Java-SQL-HTML 연습문제2
[작업할 내용 정리]
-
txt. 읽고 책제목/ 가격 / 이미지파일명 SQL에 저장
-
데이터 불러와서 total price 계산하고, HTML에 table로 출력
-
이미지 - 책제목 - 가격 순 출력
[구조짜기]
★bold&color = 자꾸 까먹는 부분&실수★
-
JDBC connect
-
build path - jdbc jar file
-
Class
-
MainClass
-
FileReadWriteClass
-
DB Connection
-
Connection = null; // import Connection.class
-
driveName / db_uri / db_id / db_pw
-
Class.forName(driveName);
-
DriverManager.getConnection(uri, id, pw); // import DriverManagerClass
-
Exceptions
-
new DB / table - SQL
-
use testdb;
-
create table books (name varchar(20), price varchar(10), imgname varchar(20));
-
alter table 테이블명 modify 컬럼명 varchar(사이즈);
-
name varchar size 잘못 정해서 error..
-
desc books; // check
-
FileReader / BufferedReader - txt
-
FileReader fr = new FileReader(txt_uri);
-
BufferedReader br = new BufferedReader(fr);
-
lines = "";
-
while(!(lines==null)) { lines=br.readLine(); String x[] = line.split(","); }
-
br.close();
-
Exceptions
-
insert data into SQL
-
Statement stmt = this.conn.createStatement
-
int n = stmt.executeUpdate("insert into books value('x[0]', 'x[1]', 'x[2]')");
-
ResultSet
-
ResultSet =null;
-
try 밖에서 초기화 해줘야 return!
-
Statement stmt = executeQuery("select * from books");
-
return ResultSet
-
Exception
-
+e.getMessage()
-
HTMLReadWriteClass
-
public static void writeHTML(ResultSet rs){}
-
FileWriter / BufferedWriter - HTML
-
bw.write(<"<html>">); ~~~ bw.write(<"</html>">);
-
bw.write("Total Price : "+totalPrice);
-
bw.write(<table><tr><td>Book Cover</td><td>Pricce</td><td>Title</td></tr>)
-
rs.beforeFirst();
-
while(rs.next()) { bw.write("<tr><td> rs.getString("name")</td> <td>rs.getString("price")</td> <td>rs.getString("imgname")</td> </tr>");
-
int totalPrice += Integer.paseInt(rs.getString("price"));}
-
Integer.paseInt() 문법!
-
bw.close();
-
bw.write(</table>)
-
Exceptions // 일단 여기까지!
-
public static void readHTML() {}
[My Solution]
MainClass
FileReadWriteClass
1234567891011121314151617181920212223 import java.sql.ResultSet;public class MainClass {public static void main(String[] args) {FileReadWriteClass fc = new FileReadWriteClass();fc.connect();System.out.println("\n----------\n");//fc.data_toSQL();//System.out.println("\n----------\n"// + "Show Result Set");ResultSet rs = fc.showResult();System.out.println("\n----------\n");HTMLWriteReadClass.writeHTML(rs);}}cs
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class FileReadWriteClass {Connection conn = null;public FileReadWriteClass() {}public void connect() {String driveName = "com.mysql.jdbc.Driver";String db_url = "jdbc:mysql://localhost:3306/testdb";String db_id = "root";String db_pw = "1234";try {Class.forName(driveName);System.out.println("Success__Driver Connection_");conn = DriverManager.getConnection(db_url, db_id, db_pw);System.out.println("Success__DB Loading_");} catch (ClassNotFoundException e) {System.out.println("ERR : Driver Connection ERROR_" + e.getMessage());} catch (SQLException e) {System.out.println("ERR : DB Loading ERROR_" + e.getMessage());}} // connect()public void data_toSQL() {String txt_uri = "C:/Users/USER/Desktop/JAVA/book_data.txt";String line = "";try {FileReader fr = new FileReader(txt_uri);BufferedReader br = new BufferedReader(fr);Statement stmt = this.conn.createStatement();while (!((line = br.readLine()) == null)) {// System.out.println(line);String[] words = line.split(",");// System.out.println(words[0]+"\t"+words[1]+"\t"+words[2]);// System.out.println(words[0].length()+"\t"+words[1].length()+"\t"+words[2].length());int n = stmt.executeUpdate("insert into books value('" + words[0] + "', '" + words[1] + "', '" + words[2] + "')");// System.out.println("number of data input : "+ n );}br.close();} catch (FileNotFoundException e) {System.out.println("ERR__File doesn't EXIST_" + e.getMessage());} catch (SQLException e) {System.out.println("ERR__can't WRITE_" + e.getMessage());} catch (IOException e) {System.out.println("ERR__File can't be READ_" + e.getMessage());}} // data_toSQLpublic ResultSet showResult() {ResultSet rs = null;try {Statement stmt = this.conn.createStatement();rs = stmt.executeQuery("select * from books");// for check/*while (rs.next()) {System.out.println(rs.getString("name") + rs.getString("price") + rs.getString("imgname"));}*/} catch (SQLException e) {System.out.println("ERR_Can't Create Statement");}return rs;} // showResult()}cs
HTMLWriteReadClass
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import java.sql.ResultSet;import java.sql.SQLException;public class HTMLWriteReadClass {static String html_url = "C:/filetest/books.html";public HTMLWriteReadClass() {}public static void writeHTML(ResultSet rs) {try {FileWriter fw = new FileWriter(html_url);BufferedWriter bw = new BufferedWriter(fw);bw.write("<html>");bw.newLine();bw.write("<head><title> \"Hello, Books!\"</title></head>");bw.newLine();bw.write("<body bgcolor=#cc99ff>");bw.newLine();rs.beforeFirst();int sum = 0;while(rs.next()){sum += Integer.parseInt(rs.getString("price"));}bw.write("<h2>Total Price = " + sum+"</h2>");bw.newLine();bw.write("<table border=20 bgcolor=#f2e6ff> <tr> <th>number</th> <th>Book Cover</th> <th>Pricce</th> <th>Title</th> </tr>");bw.newLine();rs.beforeFirst();int i=1;while (rs.next()) {bw.write("<tr>"+ "<td>" +i+ "</td>"+ "<td><img src=\"./booksimages/"+rs.getString("imgname")+"\"></td>"+ "<td>" +rs.getString("price")+ "</td>"+ "<td>" +rs.getString("name")+ "</td>"+ "</tr>");bw.newLine();}bw.write("</table>");bw.newLine();bw.write("</body>");bw.newLine();bw.write("</html>");bw.newLine();bw.close();System.out.println("Finish the file writing");} catch (IOException e) {System.out.println("ERR_Can't make the FILE__" + e.getMessage());} catch (SQLException e) {System.out.println("ERR_Can't Read Result Set__" + e.getMessage());}}}cs
댓글
댓글 쓰기