0
在我正在處理的項目中,我需要執行搜索SQL查詢,即Java Netbeans中的通配符。 我能夠執行像Netbeans Java應用程序 - 在MySql數據庫上執行查詢
String driver = "jdbc:mysql://localhost/techo";
String un = "root";
String pw = "root";
String empid = id.getText();
try{
Connection con = DriverManager.getConnection(driver,un,pw);
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery("select*from employees where empid ="+empid+"");
while(rs.next())
{
String name = rs.getString("name");
String salary = rs.getString("salary");
name1.setText(name);
salary1.setText(salary);
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e);
}
簡單的查詢,這工作完全正常。但現在我想用這個MySQL查詢
Mysql>select * from employes where empid like "123%";
,而不是這個
Mysql>select * from employes where empid =123;
在Java Netbeans的
。
我試圖做到這一點
String driver = "jdbc:mysql://localhost/techo";
String un = "root";
String pw = "root";
String empid = id.getText();
try{
Connection con = DriverManager.getConnection(driver,un,pw);
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery("select*from employees where empid like "+empid%+"");
while(rs.next())
{
String id = rs.getString("EmpId");
String name = rs.getString("name");
String salary = rs.getString("salary");
area.setText(id +" "+name+" "+salary+" "+ "\n");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e);
}
正如你可以看到,在第8行,我插入通配符(%),但是這是行不通的。我該如何解決這個問題?
謝謝。它有幫助。 –