import javax.swing.table.AbstractTableModel;
public class ZipTableModel1 extends AbstractTableModel {
String[] columnNames = {"번호", "우편번호", "시도", "구군", "동", "번지"};
Object[][] data = {
{"", "", "", "", "", ""},
};
public ZipTableModel1() { }
public ZipTableModel1(Object[][] data) { this.data = data; }
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
public String getColumnName(int columnIndex) {
return columnNames[columnIndex];
}
}
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JLabel;
public class ZipSearch1 extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JScrollPane jScrollPane = null;
private JTable jTable = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(14, 47, 580, 386));
jScrollPane.setViewportView(getJTable());
}
return jScrollPane;
}
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable();
jTable.setModel(new ZipTableModel1());
}
return jTable;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(155, 15, 273, 25));
jTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
searchZipcode();
}
});
}
return jTextField;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(440, 15, 150, 25));
jButton.setText("우편번호검색");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
searchZipcode();
}
});
}
return jButton;
}
private void searchZipcode() {
String dong = jTextField.getText().trim();
if(dong.length() <= 1) {
System.out.println("1자 이상을 입력해야 합니다.");
} else {
System.out.println("입력한 동이름 : " + dong);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ZipSearch1 thisClass = new ZipSearch1();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public ZipSearch1() {
super();
initialize();
}
private void initialize() {
this.setSize(617, 468);
this.setResizable(false);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(14, 16, 132, 25));
jLabel.setText("동이름을 입력하세요");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(jLabel, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
}
import java.awt.Rectangle;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class ZipSearch2 extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JScrollPane jScrollPane = null;
private JTable jTable = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(14, 47, 580, 386));
jScrollPane.setViewportView(getJTable());
}
return jScrollPane;
}
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable();
jTable.setModel(new ZipTableModel1());
}
return jTable;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(155, 15, 273, 25));
jTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
searchZipcode();
}
});
}
return jTextField;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(440, 15, 150, 25));
jButton.setText("우편번호검색");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
searchZipcode();
}
});
}
return jButton;
}
private void searchZipcode() {
String dong = jTextField.getText().trim();
if(dong.length() <= 1) {
JOptionPane.showMessageDialog(ZipSearch2.this, "1자 이상을 입력해야 합니다.");
return;
}
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
String strUrl = "jdbc:oracle:thin:@211.183.2.35:1521:orcl";
String strId = "scott";
String strPwd = "tiger";
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(strUrl, strId, strPwd);
stmt = conn.createStatement();
String query = "select count(*) from zipcode where dong like '" + dong + "%'";
rs = stmt.executeQuery(query);
int totalData = 0;
if(rs.next() & rs.getInt(1) != 0) {
Object[][] data = new Object[rs.getInt(1)][6];
query = "select * from zipcode where dong like '" + dong + "%'";
rs = stmt.executeQuery(query);
int i = 0;
while(rs.next()) {
data[i][0] = rs.getInt(1);
data[i][1] = rs.getString(2);
data[i][2] = rs.getString(3);
data[i][3] = rs.getString(4);
data[i][4] = rs.getString(5);
data[i][5] = rs.getString(6);
i++;
}
jTable.setModel(new ZipTableModel1(data));
} else {
JOptionPane.showMessageDialog(ZipSearch2.this, "찾아진 데이터가 없습니다.");
jTable.setModel(new ZipTableModel1());
}
} catch (ClassNotFoundException e) {
System.out.println("드라이버 로드 실패");
} catch (SQLException e) {
System.out.println("데이터베이스 연결 실패 : " + e.toString());
} finally {
if(rs != null) try { rs.close(); } catch(SQLException e) {}
if(stmt != null) try { stmt.close(); } catch(SQLException e) {}
if(conn != null) try { conn.close(); } catch(SQLException e) {}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ZipSearch2 thisClass = new ZipSearch2();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public ZipSearch2() {
super();
initialize();
}
private void initialize() {
this.setSize(617, 468);
this.setResizable(false);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(14, 16, 132, 25));
jLabel.setText("동이름을 입력하세요");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(jLabel, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
}
'Study > Programming' 카테고리의 다른 글
자바 Swing 달력 스케줄 관리 (0) | 2009.05.25 |
---|---|
자바 데이터 변경 (0) | 2009.05.25 |
jdbc (0) | 2009.05.25 |
자바 Swing 달력 (0) | 2009.05.22 |
자바 File IO 받은 문자 출력, 파일 생성/삭제/변경 (0) | 2009.05.22 |