简体   繁体   English

如何将 integer 数据从数据库带到 swing 用户界面中的 JTextField

[英]How to bring a integer data from database to a JTextField in swing user interface

I tried making a swing user interface component which have two TextField one take the name of product for which it returns the price of product present in database.我尝试制作一个 swing 用户界面组件,它有两个 TextField 一个取产品名称,它返回数据库中存在的产品价格。 There are some mistakes in code please refer some solution.代码中有一些错误,请参考一些解决方案。 code is as follow:代码如下:

class Swingui implements ActionListener
{
    JFrame jf=new JFrame("PRODUCT DETAILS");
    JLabel jl1=new JLabel("Product:");
    JTextField jtf1=new JTextField();
    JTextField jtf2=new JTextField();
    JLabel jl2=new JLabel("Price:");
    JButton jb=new JButton("enter");
    Swingui()
    {
        jf.setSize(500,500);
        jf.setVisible(true);
        jf.setLayout(null);
        jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
        jl1.setBounds(50,70,100,30);
        jf.add(jl1);
        jtf1.setBounds(50,100,100,30);
        jf.add(jtf1);
        jl2.setBounds(50,150,100,30);
        jf.add(jl2);
        jb.setBounds(50,250,100,30);
        jf.add(jb);
        jtf2.setBounds(50,200,100,30);
        jf.add(jtf2);
        jb.addActionListener(this);
    }
    public static void main(String arg[])
    {
        new Swingui();
    }

    public void actionPerformed(ActionEvent ae){
        try 
        {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url="jdbc:mysql://localhost/product";
            Connection con=DriverManager.getConnection(url,"root","");
            /*error*/Statement stmt=con.createStatement();
            String qry="select price from price where good='"+jtf1.getText().toString()+"'";
            /*error*/ResultSet resultSet= stmt.executeQuery(qry);
            int price = resultSet.getInt("price");
            JLabel jl3=new JLabel();
            String str;
            str=String.valueOf(price);
            jl3.setText(str);
            jl3.setBounds(50,300,100,50);
            jf.add(jl3);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

MySQL database details are: MySQL 数据库详细信息为:

         database: product
         table : price
         columns are 
         good      price
         ==============
         gold10gm   35000
         dollar       78

Add the localhost port number in the URL.在 URL 中添加 localhost 端口号。 The resultSet maintains a cursor to the current row of data fetched. resultSet 为当前获取的数据行维护一个 cursor。 And initially, the cursor will be pointing before the current row and so you cannot directly fetch the results from the result set.最初,cursor 将指向当前行之前,因此您无法直接从结果集中获取结果。 Try iterating it before accessing the values.在访问值之前尝试迭代它。

ResultSet rs = stmt.executeQuery(query);
while(rs.next())
    rs.getInt("price");
   String qry="select * from price where good= ? ";
   PreparedStatement stmt=con.prepareStatement(qry);
   stmt.SetString(jtf1.getText().toString);
   stmt.execute();
   
   ResultSet resultSet= stmt.getResultSet();
   while (resultSet.next()) {
     int price = resultSet.getInt("price");
   }

   JLabel jl3=new JLabel();
   String str;
   str=Integer.toString(price);
   jl3.setText(str);
   stmt.close(); // close the prepare statement if you don't need it further

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM