在Java GUI中使用数据库获取字符串

时间:2019-07-18 15:18:45

标签: java jdbc

使用GUI中数据库中的where Column(来自数据库)获取数据。

private void jcomboActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:

    String select = jcombo.getSelectedItem().toString();
    text1.setText(select);

    /////////////////////////////////////////////////////////
    Connection con;
    PreparedStatement pst;
    ResultSet rs;
    int id = 0;

    String Descripion=jTextArea1.getText();
    String Price = jTextField2.getText();

    try {
        Class.forName("org.apache.derby.jdbc.ClientDriver");
        con=DriverManager.getConnection("jdbc:derby://localhost:1527/demo","root","root");
        String query=("select DESCRIPTION , PACKPRICE from COMBOBOX where PACKAGE=?");
        PreparedStatement ps=con.prepareStatement(query);
        // ps.setString(1, Package);
        String Package=jcombo.getSelectedItem().toString();
        ps.setString(1, Package);
        rs=ps.executeQuery();

        while(rs.next()) {
            // ps.setString(1, rs.getString(id));
            // ps.setString(1, rs.getString(Package)

            Descripion=rs.getString("DESCRIPTION");
            Price=rs.getString("PACKPRICE");
        }
    } 
    catch(ClassNotFoundException ex) {
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
    } 
    catch(SQLException ex) {
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
}  

我需要使用数据库中名为“ Package”的1列分别获取文本字段和文本区域中的数据。

数据库包含:

  

id |包装描述包装价格
  1 | T | T | 20

0 个答案:

没有答案