如何使用java命令在数据库中搜索?(How to search in the database using java commands?)
优质
小牛编辑
127浏览
2023-12-01
问题描述 (Problem Description)
如何搜索表格的内容?
解决方案 (Solution)
以下方法使用sql命令来搜索数据库。
import java.sql.*;
public class jdbcConn {
public static void main(String[] args) throws Exception {
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con = DriverManager.getConnection(
"jdbc:derby://localhost:1527/testDb","username", "password");
Statement stmt = con.createStatement();
String query[] = {
"SELECT * FROM emp where id = 1",
"select name from emp where name like 'ravi_'",
"select name from emp where name like 'ravi%'"
};
for(String q : query) {
ResultSet rs = stmt.executeQuery(q);
System.out.println("Names for query "+q+" are");
while (rs.next()) {
String name = rs.getString("name");
System.out.print(name+" ");
}
System.out.println();
}
}
}
结果 (Result)
上面的代码示例将产生以下结果。结果可能会有所不同。
Names for query SELECT * FROM emp where id=1 are
ravi
Names for query select name from emp where name like 'ravi_' are
ravi2 ravi3
Names for query select name from emp where name like 'ravi%' are
ravi ravi2 ravi3 ravi123 ravi222