在java中执行SQL语句要特别注意通配符的问题,否则是不会成功的:
在ACCESS中的通配符是*
例:select * from student where name like '张*'
在标准的SQL语言中的通配符是%
例:select * from student where name like '张%'
在java中要执行上述的功能必须是
select * from student where name like '张%%'
举例:
import java.sql.*;
public class DB {
public static void main(String[] args) {
try {
String url = "jdbc:odbc:driver={Microsoft Access Driver(*.mdb)};DBQ=E://student";
String user = "admin";
String pwd = "root";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(url, user, pwd);
Statement stmt = conn.createStatement();
ResultSet rs = stmt
.executeQuery("select * from student where name like '张%%'");
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}