public interface Statement extends Wrapper {}
public interface PreparedStatement extends Statement {}
public interface CallableStatement extends PreparedStatement {}1
2
3
Statement是用来执行不带参数的SQL语句。
Connection conn = null;
ResultSet rs = null;
Statement statement = null;
try {
//加载
Class.forName(driver);
//连接
conn = DriverManager.getConnection(url, username, password);
//设置查询语句
statement = conn.createStatement();
statement.execute("select * from emp");
//执行查询
rs = statement.getResultSet();
while(rs.next()){
System.out.println(rs.getObject(2)+"==>"+rs.getObject(1));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
PreparedStatement是用来执行带输入参数的SQL语句。
Connection conn = null;
ResultSet rs = null;
PreparedStatement statement = null;
try {
//加载
Class.forName(driver);
//连接
conn = DriverManager.getConnection(url, username,password);
//设置查询语句
statement = conn.prepareStatement("select * from emp where ename like ?");
//设置参数
statement.setString(1,"%S%");
//执行查询
rs = statement.executeQuery();
while(rs.next()){
System.out.println(rs.getObject(2)+"==>"+rs.getObject(1));
}
}catch(){
}
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx