发布网友 发布时间:2022-05-29 16:46
共3个回答
热心网友 时间:2023-10-25 23:59
PreparedStatement pstm = conn.PreparedStatement("select * from t where t.name like ?");追答ublic class Shbk{
public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
public static final String dburl =
"jdbc:mysql://localhost:3306/wang";
public static final String dbuser = "root";
public static final String dbpass = "wang";
public Shbk(String name,String book,String writer)throws Exception{
Connection coon=null;
PreparedStatement psmt=null;
ResultSet rs =null;
JFrame frame =new JFrame("查询");
String[] titles={"id","姓名","书名","作者"};
Object[][] userinfo=new Object[10][4];
JButton but=new JButton("确定");
String sql="SELECT*"+" FROM library WHRERE name LIKE ? OR book
LIKE ? OR writer LIKE ?";
Class.forName(DBDRIVER);
coon=DriverManager.getConnection(dburl,dbuser,dbpass);
psmt=coon.prepareStatement(sql);
psmt.setString(1,"‘%"+name+"%’");
psmt.setString(2,"‘%"+book+"%’");
psmt.setString(3,"‘%"+writer+"%’");
rs=psmt.executeQuery();
for(int i=0;rs.next();i++){
userinfo[i][0]=rs.getInt(1);
userinfo[i][1]=rs.getString(2);
userinfo[i][2]=rs.getString(3);
userinfo[i][3]=rs.getString(4);
}
热心网友 时间:2023-10-25 23:59
like '%关键字%'追问如果关键字是用户输进去的,不是常量,怎么办?追答字符串拼接,
String str=""//用户输入
sql=" select * from table where xxx like'%"+str+"%'"
热心网友 时间:2023-10-25 23:59
... like '%用户输入的关键字%'