JAVA 데이터베이스 실습 예제 UI

Programming/JAVA,JSP 2013. 2. 24. 12:51 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

 

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JList;

import javax.swing.JPanel;


class UI extends JFrame{

    JPanel panel1;

    JPanel panel2;

    JPanel panel3;

    JPanel panel4;

    JList list1;

    JList list2;

    JList list3;

    JList list4;

    JButton updateButton;

   

    public UI()

    {

        JLabel label1 = new JLabel("book_id");

        JLabel label2 = new JLabel("title");

        JLabel label3 = new JLabel("publi");

        JLabel label4 = new JLabel("price");

        String[] listItem1 = { "green", "red", "orange", "dark blue" };

        String[] listItem2 = { "green", "red", "orange", "dark blue" };

        String[] listItem3 = { "green", "red", "orange", "dark blue" };

        String[] listItem4 = { "green", "red", "orange", "dark blue" };

        list1 = new JList(listItem1);

        list2 = new JList(listItem2);

        list3 = new JList(listItem3);

        list4 = new JList(listItem4);

        updateButton = new JButton("Update");

        panel1 = new JPanel(new BorderLayout());

        panel2 = new JPanel(new BorderLayout());

        panel3 = new JPanel(new BorderLayout());

        panel4 = new JPanel(new BorderLayout());

       

        setSize(600,400);

        setVisible(true);

        setLayout(new GridLayout(0, 5));

        add(panel1);

        add(panel2);

        add(panel3);

        add(panel4);

        add(updateButton);

        panel1.add(label1,BorderLayout.NORTH);

        panel2.add(label2,BorderLayout.NORTH);

        panel3.add(label3,BorderLayout.NORTH);

        panel4.add(label4,BorderLayout.NORTH);

        panel1.add(list1,BorderLayout.CENTER);

        panel2.add(list2,BorderLayout.CENTER);

        panel3.add(list3,BorderLayout.CENTER);

        panel4.add(list4,BorderLayout.CENTER);

    }

   

}

public class Test {

       public static Connection makeConnection()

       {

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

             String id = "root";

             String password = "green";

             Connection con = null;

             try {

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

                    System.out.println("드라이버 적재 성공");

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

                    System.out.println("데이터베이스 연결 성공");

             } catch (ClassNotFoundException e) {

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

             } catch (SQLException e) {

                    System.out.println("연결에 실패하였습니다.");

             }

             return con;

       }

       public static void main(String arg[]) throws SQLException {

           UI ui = new UI();

           Connection con = makeConnection();

           Statement stmt = con.createStatement();

           ResultSet rs = stmt.executeQuery("SELECT * FROM books");

           while (rs.next()) {

                  int id = rs.getInt("book_id");

                  String title = rs.getString("title");

                  System.out.println(id + " " + title);

           }

       }

}

'Programming > JAVA,JSP' 카테고리의 다른 글

JAVA JTABLE 사용 예제  (0) 2013.03.03
JAVA executeQuery INSERT DELETE  (0) 2013.03.02
JAVA JDBC 튜토리얼 예제 사이트  (0) 2013.02.24
JAVA 채팅 프로그램 + GUI  (0) 2013.02.23
RadioButton exam  (0) 2013.02.03