JAVA 레코드 이동 예제

728x90
package pack.db;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class DbTest6RecMove extends JFrame implements ActionListener{
       private JButton btnF, btnP, btnN, btnL;
       private JTextField txtNo, txtName;
       
       private Connection conn;
       private Statement stmt;
       private ResultSet rs;
       
       public DbTest6RecMove() {
              setTitle("레코드 이동");
              
              layInit();
              accDb();
              
              setBounds(200,200,300,250);
              setVisible(true);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
       
       private void layInit() { // 레이아웃
              txtNo = new JTextField(" ",5);
              txtName = new JTextField("",10);
              txtNo.setEditable(false); // 읽기만 가능하게
              txtName.setEditable(false); // 읽기만 가능하게
              
              JPanel panel1 = new JPanel();
              panel1.add(new JLabel("고객번호"));
              panel1.add(txtNo);
              panel1.add(new JLabel("이름"));
              panel1.add(txtName);
              add("North", panel1);
              
              btnF = new JButton("|<<");
              btnP = new JButton("<");
              btnN = new JButton(">");
              btnL = new JButton(">>|");
              JPanel panel2 = new JPanel();
              panel2.add(btnF);
              panel2.add(btnP);
              panel2.add(btnN);
              panel2.add(btnL);
              add("Center", panel2);
              
              btnF.addActionListener(this);
              btnP.addActionListener(this);
              btnN.addActionListener(this);
              btnL.addActionListener(this);
              
              
       }
       
       private void accDb() {
              try {
                     Class.forName("oracle.jdbc.driver.OracleDriver");
                     
                     conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
                     stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                                                                     ResultSet.CONCUR_READ_ONLY); // 레코드 방향이 역방향도 가능해짐
                     
                     String sql = "select gogek_no, gogek_name from gogek order by gogek_no asc";
                     rs = stmt.executeQuery(sql);
                     rs.next();
                     displayData();
                           
                     } catch (Exception e) {
                     System.out.println("accDb err:" + e);
                     }
       }
       
       private void displayData() {
              try {
                     txtNo.setText(rs.getString("gogek_no"));
                     txtName.setText(rs.getString("gogek_name"));
                                  
              } catch (Exception e) {
                     // System.out.println("displayDate err  : " + e);
                     JOptionPane.showMessageDialog(this, "자료의 처음 또는 끝에 도달하였습니다");
              }
       }
       
       @Override
       public void actionPerformed(ActionEvent arg0) {
              try {
                     if(arg0.getSource() == btnF) rs.first();
                     else if(arg0.getSource() == btnP) rs.previous();
                     else if(arg0.getSource() == btnN) rs.next();
                     else if(arg0.getSource() == btnL) rs.last();
                     
                     displayData();
              } catch (Exception e) {
                     // TODO: handle exception
              }
              
       }
       public static void main(String[] args) {
              new DbTest6RecMove();
       }
}



레코드 이동.zip





본 포스팅은 IT 교육기관인 KIC 캠퍼스의 지원을 받은 리포트입니다.
혹시 잘못되거나 문제 소지시 댓글 남겨주시면 조치하겠습니다.







728x90