안녕하세요 지난번에 자바 오류 관련해서 질문 올렸던 학부생입니다.
class JPanel01 extends JPanel {
public static final long serialVersionUID = 1L;
private JLabel jlb1;
private JComboBox jcb1;
private JTextArea jta1;
private JButton jbt1;
private JScrollPane jsp1;
private JTable table1;
private String salegood[] = { "상품 코드", "대분류", "중분류", "소분류", "상품명", "R 사이즈 가격", "L 사이즈 가격", "관리 직원 번호" };
private DefaultTableModel model = new DefaultTableModel(salegood, 0);
private String qmenu = "select sgcode,sgltype,sgmtype,sgstype,sgname,rprice,lprice,empno from salegoods";
private String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/pos";
private String user = new String("root");
private String passwd = new String("wnstn17");
private Connection con;
private PreparedStatement pstmt;
private ResultSet rs;
public JPanel01() {
setLayout(null);
setSize(800, 50);
setVisible(true);
jlb1 = new JLabel("상품 관리");
jlb1.setSize(90, 30);
jlb1.setLocation(10, 10);
jlb1.setVisible(true);
add(jlb1);
jcb1 = new JComboBox(salegood);
jcb1.setSize(130, 30);
jcb1.setLocation(100, 10);
jcb1.setVisible(true);
add(jcb1);
jta1 = new JTextArea();
jta1.setSize(300, 30);
jta1.setLocation(240, 10);
jta1.setVisible(true);
add(jta1);
jbt1 = new JButton("검색");
jbt1.setSize(90, 30);
jbt1.setLocation(550, 10);
jbt1.setVisible(true);
jbt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
search();
}
});
add(jbt1);
table1 = new JTable(model);
jsp1 = new JScrollPane(table1);
jsp1.setSize(630, 800);
jsp1.setLocation(10, 60);
jsp1.setVisible(true);
add(jsp1);
showtable();
}
public void search() {
try {
Class.forName(driver);
con = DriverManager.getConnection(url, "root", "wnstn17");
String keyword = jta1.getText();
int i = jcb1.getSelectedIndex();
String index = null;
switch (i) {
case 0:
index = "sgcode";
break;
case 1:
index = "sgltype";
break;
case 2:
index = "sgmtype";
break;
case 3:
index = "sgstype";
break;
case 4:
index = "sgname";
break;
case 5:
index = "rprice";
break;
case 6:
index = "lprice";
break;
case 7:
index = "empno";
break;
}
String query = "SELECT * FROM salegoods WHERE " + index + " = '" + keyword + "' ORDER BY sgcode";
pstmt = con.prepareStatement(query);
rs = pstmt.executeQuery();
while (rs.next()) {
model.setRowCount(0);
model.addRow(new Object[] { rs.getString("sgcode"), rs.getString("sgltype"), rs.getString("sgmtype"),
rs.getString("sgstype"), rs.getString("sgname"), rs.getString("rprice"), rs.getString("lprice"),
rs.getString("empno") });
}
} catch (Exception e) {
}
}
public void showtable() {
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, passwd);
pstmt = con.prepareStatement(qmenu);
rs = pstmt.executeQuery();
while (rs.next()) {
model.addRow(new Object[] { rs.getString("sgcode"), rs.getString("sgltype"), rs.getString("sgmtype"),
rs.getString("sgstype"), rs.getString("sgname"), rs.getString("rprice"), rs.getString("lprice"),
rs.getString("empno") });
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
rs.close();
pstmt.close();
con.close();
} catch (Exception e2) {
System.out.println("오류 1번");
}
}
}
}
search 부분인데.... 이게 검색을 하면 제가 콤보박스에서 항목이 여러개인 값을 넣어도 검색값은 하나만 나옵니다.
혹시 검색한 모든 투플들을 보여줄수 있는 방법이 없는지 조언 여쭙고자 질문 올렸습니다.
글을 읽어주셔서 감사합니다.