简体   繁体   中英

Reading data from mysql database using java

Firstly, I'm reading the product name and number of products from user using jTextFields. For that product I read the product id and price from database using sql query. But in the below code I display the product price in a jtextField but while running tha file I get query executed successfully but I'm not getting anything in the jtextField.

And please check the sql query and resultset use, table name is "item" and database name is "myshop", I declared variables globelly and this code is in a jButton's 'ActionPeformed" part.

String item_name=name.getText();
int item_no=Integer.parseInt(no.getText());
String sql="SELECT id,price FROM item WHERE item.name='item_name'";
try{       
Class.forName("com.mysql.jdbc.Driver");
Connection con(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/myshop","root","mysql");
java.sql.Statement stmt=con.createStatement();
if (stmt.execute(sql)) {
rs = stmt.getResultSet();
JOptionPane.showMessageDialog(this, "succes","executed query",JOptionPane.PLAIN_MESSAGE);
} else {
System.err.println("select failed");}
int idIndex   = rs.findColumn("id");
int priceIndex    = rs.findColumn("price");

while(rs.next()){
item_id=rs.getInt(idIndex);
item_price=rs.getInt(priceIndex);
jTextField1.setText(""+item_price);//displaying product price in a jTextField1
jTextField2.setText(""+item_id);//displaying product id in a jTextField2  
  }
}
catch(Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
}

This line should be

String sql="SELECT id,price FROM item WHERE item.name='item_name'";

like this

String sql="SELECT id,price FROM item WHERE item.name='"+item_name+"'";

Use a PreparedStatement so you don't have to worry about delimiting all the variables:

String sql="SELECT id, price FROM item WHERE item.name = ?";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString( 1, item_name);
ResultSet rs = stmt.executeQuery();

Then the prepared statement will replace the variable for you with the proper quotes.

您需要将 item_name 作为参数并放在引号中,

String sql="SELECT id,price FROM item WHERE item.name='"+ item_name+"'";

Try to avoid this type of mistake by using PreparedStatement

String sql="SELECT id,price FROM item WHERE item.name=?";  
PreapredStatement ps = con.prepareStatement(sql);
ps.setString(1,item_name); 
ResultSet rs = ps.executeQuery(); 

Use of PreparedStatement also prevent SQL injection attack.

try this code .

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/myshop","root","mysql");
            PreparedStatement pt=con.prepareStatement("SELECT id,price FROM item WHERE item.name=?");
            pt.setString(1,"item_name");
          ResultSet rs;
           if(pt.execute())
           {
                rs=pt.getResultSet();
               JOptionPane.showMessageDialog(this, "succes","executed query",JOptionPane.PLAIN_MESSAGE);
          }
           else {
    System.err.println("select failed");
           }

        while(rs.next()){
    item_id=rs.getInt(1);
    item_price=rs.getInt(2);
    jTextField1.setText(""+item_price);//displaying product price in a jTextField1
    jTextField2.setText(""+item_id);//displaying product id in a jTextField2    

        }

First, you need an reader, like this

private static void reader() throws SQLException {
    DataBaseName db = new DataBaseName ();
    names = db.getNames();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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