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
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

#include <iostream>

#include <string>

using namespace std;

 

int main()

{

    string str;

    string tempStr[10];

    int tempStrSeq = 0;

    int tempStrCount[10] = {0};

    int start = 0, many = 0;

    getline(cin, str, '\n');

    for( int i = 0 ; i < str.length() ; i++ )

    {

        if( str.at(i) == ' ' || str.at(i) == ',' || str.at(i) == '.' )

        {

            if( many != 0)

            {

                cout << str.substr(start,many) << endl;

                int flag = 0;

                for( int j = 0 ; j < tempStrSeq + 1 ; j++ )

                {

                    if( tempStr[j].compare(str.substr(start,many)) == 0 )

                    {

                        flag++;

                        tempStrCount[j]++;

                    }

                    else

                    {

                        // 같지않다면

                    }

                }

                if( flag == 0)

                {

                    tempStr[tempStrSeq] = str.substr(start,many);

                    tempStrCount[tempStrSeq]++;

                    tempStrSeq++;

                }

                cout << "s : "<< start << "m : " << many << endl;

                many = 0;

            }

            start = i+1;

        }

        else

        {

            many++;

        }

    }

    if( many != 0)

    {

        cout << str.substr(start,many) << endl;

        int flag = 0;

        for( int j = 0 ; j < tempStrSeq + 1 ; j++ )

        {

           if( tempStr[j].compare(str.substr(start,many)) == 0 )

            {

                flag++;

                tempStrCount[j]++;

            }

            else

            {

                // 같지않다면

            }

        }

        if( flag == 0)

        {

            tempStr[tempStrSeq] = str.substr(start,many);

            tempStrSeq++;

            tempStrCount[tempStrSeq]++;

        }

        cout << "s : "<< start << "m : " << many << endl;

    }

 

    // TODO: 계산

 

    for( int i = 0 ; i < 10 ; i++ )

    {

        cout << tempStr[i] ;

        cout << " " << tempStrCount[i] << endl;

    }

}

'Programming > C,CPP,CS' 카테고리의 다른 글

아스키코드표  (0) 2013.05.02
프로그래밍용으로 좋은 폰트  (0) 2013.03.23
CPP string 줄단위 입력  (0) 2013.01.26
링크드리스트 학생관리  (0) 2013.01.19
C언어 달력소스코드  (0) 2013.01.13