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.
首先,我正在使用jTextFields从用户那里读取产品名称和产品数量。对于该产品,我使用sql查询从数据库读取产品id和价格。但是在下面的代码中,我在jtextField中显示产品价格,但是在运行这个文件时,我成功地执行了查询,但是在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.
请检查sql查询和resultset的使用,表名是“item”,数据库名是“myshop”,我全局声明变量,这段代码在jButton的‘ActionPeformed’部分。
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());
}
5 个解决方案
#1
1
you would need to take item_name as param and put in quotes,
你需要将item_name作为参数并加上引号,
String sql="SELECT id,price FROM item WHERE item.name='"+ item_name+"'";
#2
3
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+"'";
#3
3
Use a PreparedStatement
so you don't have to worry about delimiting all the variables:
使用一个PreparedStatement,这样你就不用担心所有变量的限制:
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.
然后,准备好的语句将用正确的引号替换变量。
#4
0
Try to avoid this type of mistake by using PreparedStatement
通过使用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.
使用PreparedStatement也可以防止SQL注入攻击。
#5
0
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
}
#1
1
you would need to take item_name as param and put in quotes,
你需要将item_name作为参数并加上引号,
String sql="SELECT id,price FROM item WHERE item.name='"+ item_name+"'";
#2
3
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+"'";
#3
3
Use a PreparedStatement
so you don't have to worry about delimiting all the variables:
使用一个PreparedStatement,这样你就不用担心所有变量的限制:
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.
然后,准备好的语句将用正确的引号替换变量。
#4
0
Try to avoid this type of mistake by using PreparedStatement
通过使用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.
使用PreparedStatement也可以防止SQL注入攻击。
#5
0
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
}