JAVA Mysql + JTable + File

실습과제 모음 2012. 7. 29. 17:27 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.


data.txt

import java.awt.BorderLayout;

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;

import java.util.ArrayList;

 

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable;

 

class data {

 

    public String seNum; // 참가번호

    public String seDate; // 참가 신청일

    public String Age; // 나이

    public String Time; // 기록시간

 

    public String toString() {

        return seNum + " " + seDate + " " + Age + " " + Time;

    }

}

 

class MyFrame extends JFrame {

    JTable table;

    ArrayList<data> arrTest;

    Object[][] tableData;

 

    MyFrame() throws IOException {

        String[] title = { "참가번호", "참가 신청일", "나이", "기록시간" };

        fileLoad();

        dataTranslation();

        table = new JTable(tableData, title);

        add(table);

        setSize(600, 400);

        setVisible(true);

    }

 

    void dataTranslation() {

        tableData = new Object[arrTest.size()][4];

        for (int i = 0; i < arrTest.size(); i++) {

           tableData[i][0] = arrTest.get(i).seNum;

           tableData[i][1] = arrTest.get(i).seDate;

           tableData[i][2] = arrTest.get(i).Age;

           tableData[i][3] = arrTest.get(i).Time;

        }

    }

 

    void fileLoad() throws IOException {

        arrTest = new ArrayList<data>();

        FileReader in = null;

        in = new FileReader("data.txt");

        int c;

        String seNum = ""; // 참가번호

        String seDate = ""; // 참가 신청일

        String Age = ""; // 나이

        String Time = ""; // 기록시간

        int i = 0;

        while ((c = in.read()) != -1) {

           System.out.print((char) c);

           switch (i) {

           case 0:

               seNum += (char) c;

               break;

           case 1:

               seDate += (char) c;

               break;

           case 2:

               Age += (char) c;

               break;

           case 3:

               Time += (char) c;

               break;

           }

           if (c == '\t') {

               i++;

           } else if (c == '\n') {

               data temp = new data();

               temp.Age = Age;

               temp.seDate = seDate;

               temp.seNum = seNum;

               temp.Time = Time;

               arrTest.add(temp);

               i = 0;

               seNum = "";

               seDate = "";

               Age = "";

               Time = "";

           }

        }

        for (int j = 0; j < arrTest.size(); j++) {

            System.out.println(arrTest.get(j).toString());

        Test.addBook(arrTest.get(j).seNum,arrTest.get(j).seDate

                   , arrTest.get(j).Age, arrTest.get(j).Time);

        }

        //public static void addBook

        //(String senum, String sedate, String age,

        //String time) {

    }

}

 

public class Test {

    static Connection con = null;

 

    static void makeConnection() {

        try {

           Class.forName("com.mysql.jdbc.Driver");

        } catch (ClassNotFoundException e) {

           System.out.println("드라이버를 찾을 없습니다");

        }

       String url = "jdbc:mysql://localhost/book_db";

        String user = "root";

        String password = "green";

        try {

           con = DriverManager.getConnection(url, user, password);

        } catch (SQLException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

        }

    }

 

    public static void addBook(String senum, String sedate, String age,

           String time) {

        try {

           Statement stmt = con.createStatement();

           String s = "INSERT INTO temp (senum , sedate, age, time ) VALUES ";

           s += "('" + senum + "','" + sedate + "','" + age + "','"

                   + time + "')";

           System.out.println(s);

           int i = stmt.executeUpdate(s);

           if (i == 1)

               System.out.println("레코드 추가 성공");

           else

               System.out.println("레코드 추가 실패");

        } catch (SQLException e) {

           System.out.println(e.getMessage());

           System.exit(0);

        }

    }

 

 

    public static void main(String[] args) throws IOException {

        makeConnection();

        MyFrame frame = new MyFrame();

    }

}

 

'실습과제 모음' 카테고리의 다른 글

CPP 게임 실습  (0) 2012.09.22
C언어 실습문제 0811  (0) 2012.08.11
JAVA JFrame + MySql 추가 실습예제  (0) 2012.07.28
JAVA SQL + JFrame  (0) 2012.07.28
JAVA Mysql 연결 실습  (0) 2012.07.28