1.每一种数据库都会尽最大努力对预编译语句提供最大的性能优化.因为预编译语句有可能被重复调用.所以语句在被DB的编译器编译后的执行代码被缓存下来,那么下次调用时只要是相同的预编译语句就不需要编译,只要将参数直接传入编译过的语句执行代码中(相当于一个涵数)就会得到执行.这并不是说只有一个Connection中多次执行的预编译语句被缓存,而是对于整个DB中,只要预编译的语句语法和缓存中匹配.那么在任何时候就可以不需要再次编译而可以直接执行.
2.PreparedStatement在conn.prepareStatement(sql)时就把sql语句传给它,这样它会在数据库端进行预编译(包含占位符),下次execute或者executeQuery时只要是相同的预编译语句就不需要编译,只要将参数直接传入编译过的语句执行代码中(相当于一个涵数)就会得到执行。其实这并不是说只有一个Connection中多次执行的预编译语句被缓存,这是PreparedStatement借助数据库的编译sql语句的原理来实现的优先做法而已。
Statement在conn.createStatement()时不传sql语句,而是在execute或者executeQuery时传过去死的sql语句。这样使是相同一操作,而由于每次操作的数据不同所以使整个语句相匹配的机会极小,几乎不太可能匹配.
3.使用Statement要给它传死的拼接的sql语句,其实这样做是很不安全的,有发生恶意sql语句注入的危险。比如:
String sql="select * from t_user where name='zhangs' and passwd='zhangs123'"
而我恶意给你注入一个"or 1=1",就成了
String sql="select * from t_user where name='zhangs' and passwd='zhangs123' or 1=1"
这样,你的密码就失去了功效。
小结,PreparedStatement在使用上的优势是显而易见的,当然,它的开销会比Statement达一些,但我觉得功能第一,任何情况下还是首选PreparedStatement。
String sql="select * from t_user where name='zhangs' and passwd='zhangs123'"
而我恶意给你注入一个"or 1=1",就成了
String sql="select * from t_user where name='zhangs' and passwd='zhangs123' or 1=1"
这样,你的密码就失去了功效。
小结,PreparedStatement在使用上的优势是显而易见的,当然,它的开销会比Statement达一些,但我觉得功能第一,任何情况下还是首选PreparedStatement。
下面是我分别用两个Statement和PreparedStatement写的用来增删改查的操作:
/**
* Statement是先用Connection得到一个空的执行器,在执行的时候给它传拼好的死的sql
* @author Administrator
*
*/
public class StatementCRUDtest {
/**
* 操作表jdbc_users
* @param args
*/
public static void main(String[] args) {
User u=new User();
u.setId(45);
u.setName("statement");
u.setPasswd("yf123");
u.setPhone("13821930");
u.setEmail("yf@163.com");
//insert(u);
//delete(2);
//reset(u);
System.out.println(getById(45));
}
/**增*/
public static void insert(User user){
Connection conn=null;
Statement stmt=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123");
stmt=conn.createStatement();//Statement创建时就是一个空的执行器
/**在execute或者executeQuery时执行死的sql语句*/
/**这只能是拼好的字符串,而不能动态的传参数,并且在数据库中每次肯定穿的是不同的sql语句,因此每次都要解析编译*/
stmt.execute("insert into jdbc_users values ("+user.getId()+",'"+user.getName()+"','"+user.getPasswd()+"','"+user.getPhone()+"','"+user.getEmail()+"')");
} catch (Exception e) {
e.printStackTrace();
}finally{
if(stmt!=null){try{stmt.close();}catch(Exception e){}}
if(conn!=null){try{conn.close();}catch(Exception e){}}
}
}
/**删*/
public static void delete(Integer id){
Connection conn=null;
Statement stmt=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123");
stmt=conn.createStatement();//Statement创建时就是一个空的执行器
/**在execute或者executeQuery时执行死的sql语句*/
/**这只能是拼好的字符串,而不能动态的传参数,并且在数据库中每次肯定穿的是不同的sql语句,因此每次都要解析编译*/
stmt.execute("delete from jdbc_users where id="+id);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(stmt!=null){try{stmt.close();}catch(Exception e){}}
if(conn!=null){try{conn.close();}catch(Exception e){}}
}
}
/**改*/
public static void reset(User user){
Connection conn=null;
Statement stmt=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123");
stmt=conn.createStatement();//Statement创建时就是一个空的执行器
/**在execute或者executeQuery时执行死的sql语句*/
/**这只能是拼好的字符串,而不能动态的传参数,并且在数据库中每次肯定穿的是不同的sql语句,因此每次都要解析编译*/
String sql="update jdbc_users set name='"+user.getName()+"',passwd='"+user.getPasswd()+"',phone='"+user.getPhone()+"',email='"+user.getEmail()+"' where id="+user.getId();
System.out.println(sql);
stmt.execute(sql);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(stmt!=null){try{stmt.close();}catch(Exception e){}}
if(conn!=null){try{conn.close();}catch(Exception e){}}
}
}
/**查*/
public static User getById(Integer id){
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
User u=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123");
stmt=conn.createStatement();//Statement创建时就是一个空的执行器
/**在execute或者executeQuery时执行死的sql语句*/
/**这只能是拼好的字符串,而不能动态的传参数,并且在数据库中每次肯定穿的是不同的sql语句,因此每次都要解析编译*/
String sql="select * from jdbc_users where id="+id;
System.out.println(sql);
rs=stmt.executeQuery(sql);
while(rs.next()){
u=new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
u.setPhone(rs.getString("phone"));
u.setPasswd(rs.getString("passwd"));
u.setEmail(rs.getString("email"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(stmt!=null){try{stmt.close();}catch(Exception e){}}
if(conn!=null){try{conn.close();}catch(Exception e){}}
}
return u;
}
}
* Statement是先用Connection得到一个空的执行器,在执行的时候给它传拼好的死的sql
* @author Administrator
*
*/
public class StatementCRUDtest {
/**
* 操作表jdbc_users
* @param args
*/
public static void main(String[] args) {
User u=new User();
u.setId(45);
u.setName("statement");
u.setPasswd("yf123");
u.setPhone("13821930");
u.setEmail("yf@163.com");
//insert(u);
//delete(2);
//reset(u);
System.out.println(getById(45));
}
/**增*/
public static void insert(User user){
Connection conn=null;
Statement stmt=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123");
stmt=conn.createStatement();//Statement创建时就是一个空的执行器
/**在execute或者executeQuery时执行死的sql语句*/
/**这只能是拼好的字符串,而不能动态的传参数,并且在数据库中每次肯定穿的是不同的sql语句,因此每次都要解析编译*/
stmt.execute("insert into jdbc_users values ("+user.getId()+",'"+user.getName()+"','"+user.getPasswd()+"','"+user.getPhone()+"','"+user.getEmail()+"')");
} catch (Exception e) {
e.printStackTrace();
}finally{
if(stmt!=null){try{stmt.close();}catch(Exception e){}}
if(conn!=null){try{conn.close();}catch(Exception e){}}
}
}
/**删*/
public static void delete(Integer id){
Connection conn=null;
Statement stmt=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123");
stmt=conn.createStatement();//Statement创建时就是一个空的执行器
/**在execute或者executeQuery时执行死的sql语句*/
/**这只能是拼好的字符串,而不能动态的传参数,并且在数据库中每次肯定穿的是不同的sql语句,因此每次都要解析编译*/
stmt.execute("delete from jdbc_users where id="+id);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(stmt!=null){try{stmt.close();}catch(Exception e){}}
if(conn!=null){try{conn.close();}catch(Exception e){}}
}
}
/**改*/
public static void reset(User user){
Connection conn=null;
Statement stmt=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123");
stmt=conn.createStatement();//Statement创建时就是一个空的执行器
/**在execute或者executeQuery时执行死的sql语句*/
/**这只能是拼好的字符串,而不能动态的传参数,并且在数据库中每次肯定穿的是不同的sql语句,因此每次都要解析编译*/
String sql="update jdbc_users set name='"+user.getName()+"',passwd='"+user.getPasswd()+"',phone='"+user.getPhone()+"',email='"+user.getEmail()+"' where id="+user.getId();
System.out.println(sql);
stmt.execute(sql);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(stmt!=null){try{stmt.close();}catch(Exception e){}}
if(conn!=null){try{conn.close();}catch(Exception e){}}
}
}
/**查*/
public static User getById(Integer id){
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
User u=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123");
stmt=conn.createStatement();//Statement创建时就是一个空的执行器
/**在execute或者executeQuery时执行死的sql语句*/
/**这只能是拼好的字符串,而不能动态的传参数,并且在数据库中每次肯定穿的是不同的sql语句,因此每次都要解析编译*/
String sql="select * from jdbc_users where id="+id;
System.out.println(sql);
rs=stmt.executeQuery(sql);
while(rs.next()){
u=new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
u.setPhone(rs.getString("phone"));
u.setPasswd(rs.getString("passwd"));
u.setEmail(rs.getString("email"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(stmt!=null){try{stmt.close();}catch(Exception e){}}
if(conn!=null){try{conn.close();}catch(Exception e){}}
}
return u;
}
}
/**
* PreparedStatement是在创建pstm的时候就给它传一个动态的sql,参数是通过pstm设置的。执行时,只需要空执行一下就可以.
* @author Administrator
*
*/
public class PreparedStatementCRUDtest {
/**
* 操作表jdbc_users
* @param args
*/
public static void main(String[] args) {
User u=new User();
u.setId(21);
u.setName("statement");
u.setPasswd("yf123");
u.setPhone("13821930");
u.setEmail("yf@163.com");
//insert(u);
//delete(42);
//reset(u);
System.out.println(getById(21));
}
/**增*/
public static void insert(User user){
Connection conn=null;
PreparedStatement pstmt=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123");
String sql="insert into jdbc_users values(?,?,?,?,?)";
pstmt=conn.prepareStatement(sql);//PreparedStatement创建时就传过去一个sql语句,这样就可以预编译
/**然后设置sql中好占位符的值,这里是动态的传参数*/
pstmt.setInt(1, user.getId());
pstmt.setString(2, user.getName());
pstmt.setString(3, user.getPasswd());
pstmt.setString(4, user.getPhone());
pstmt.setString(5, user.getEmail());
/**设置好后,就全封装到pstm里了,只要空执行就可以了*/
pstmt.execute();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(pstmt!=null){try{pstmt.close();}catch(Exception e){}}
if(conn!=null){try{conn.close();}catch(Exception e){}}
}
}
/**删*/
public static void delete(Integer id){
Connection conn=null;
PreparedStatement pstmt=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123");
String sql="delete from jdbc_users where id=?";
/**PreparedStatement创建时就传过去一个sql语句,这样就可以预编译*/
pstmt=conn.prepareStatement(sql);
/**然后设置sql中好占位符的值,这里是动态的传参数*/
pstmt.setInt(1, id);
/**设置好后,就全封装到pstm里了,只要空执行就可以了*/
pstmt.execute();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(pstmt!=null){try{pstmt.close();}catch(Exception e){}}
if(conn!=null){try{conn.close();}catch(Exception e){}}
}
}
/**改*/
public static void reset(User u){
Connection conn=null;
PreparedStatement pstmt=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123");
String sql="update jdbc_users set name=?,passwd=?,phone=?,email=? where id=?";
/**PreparedStatement创建时就传过去一个sql语句,这样就可以预编译*/
pstmt=conn.prepareStatement(sql);
/**然后设置sql中好占位符的值,这里是动态的传参数*/
pstmt.setString(1, u.getName());
pstmt.setString(2, u.getPasswd());
pstmt.setString(3, u.getPhone());
pstmt.setString(4, u.getEmail());
pstmt.setInt(5, u.getId());
/**设置好后,就全封装到pstm里了,只要空执行就可以了*/
pstmt.execute();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(pstmt!=null){try{pstmt.close();}catch(Exception e){}}
if(conn!=null){try{conn.close();}catch(Exception e){}}
}
}
/**查*/
public static User getById(Integer id){
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
User u=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123");
String sql="select * from jdbc_users where id=?";
pstmt=conn.prepareStatement(sql);//Statement创建时就是一个空的执行器
/**在execute或者executeQuery时执行死的sql语句*/
/**这只能是拼好的字符串,而不能动态的传参数,并且在数据库中每次肯定穿的是不同的sql语句,因此每次都要解析编译*/
pstmt.setInt(1, id);
rs=pstmt.executeQuery();
while(rs.next()){
u=new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
u.setPhone(rs.getString("phone"));
u.setPasswd(rs.getString("passwd"));
u.setEmail(rs.getString("email"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(pstmt!=null){try{pstmt.close();}catch(Exception e){}}
if(conn!=null){try{conn.close();}catch(Exception e){}}
}
return u;
}
}
* PreparedStatement是在创建pstm的时候就给它传一个动态的sql,参数是通过pstm设置的。执行时,只需要空执行一下就可以.
* @author Administrator
*
*/
public class PreparedStatementCRUDtest {
/**
* 操作表jdbc_users
* @param args
*/
public static void main(String[] args) {
User u=new User();
u.setId(21);
u.setName("statement");
u.setPasswd("yf123");
u.setPhone("13821930");
u.setEmail("yf@163.com");
//insert(u);
//delete(42);
//reset(u);
System.out.println(getById(21));
}
/**增*/
public static void insert(User user){
Connection conn=null;
PreparedStatement pstmt=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123");
String sql="insert into jdbc_users values(?,?,?,?,?)";
pstmt=conn.prepareStatement(sql);//PreparedStatement创建时就传过去一个sql语句,这样就可以预编译
/**然后设置sql中好占位符的值,这里是动态的传参数*/
pstmt.setInt(1, user.getId());
pstmt.setString(2, user.getName());
pstmt.setString(3, user.getPasswd());
pstmt.setString(4, user.getPhone());
pstmt.setString(5, user.getEmail());
/**设置好后,就全封装到pstm里了,只要空执行就可以了*/
pstmt.execute();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(pstmt!=null){try{pstmt.close();}catch(Exception e){}}
if(conn!=null){try{conn.close();}catch(Exception e){}}
}
}
/**删*/
public static void delete(Integer id){
Connection conn=null;
PreparedStatement pstmt=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123");
String sql="delete from jdbc_users where id=?";
/**PreparedStatement创建时就传过去一个sql语句,这样就可以预编译*/
pstmt=conn.prepareStatement(sql);
/**然后设置sql中好占位符的值,这里是动态的传参数*/
pstmt.setInt(1, id);
/**设置好后,就全封装到pstm里了,只要空执行就可以了*/
pstmt.execute();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(pstmt!=null){try{pstmt.close();}catch(Exception e){}}
if(conn!=null){try{conn.close();}catch(Exception e){}}
}
}
/**改*/
public static void reset(User u){
Connection conn=null;
PreparedStatement pstmt=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123");
String sql="update jdbc_users set name=?,passwd=?,phone=?,email=? where id=?";
/**PreparedStatement创建时就传过去一个sql语句,这样就可以预编译*/
pstmt=conn.prepareStatement(sql);
/**然后设置sql中好占位符的值,这里是动态的传参数*/
pstmt.setString(1, u.getName());
pstmt.setString(2, u.getPasswd());
pstmt.setString(3, u.getPhone());
pstmt.setString(4, u.getEmail());
pstmt.setInt(5, u.getId());
/**设置好后,就全封装到pstm里了,只要空执行就可以了*/
pstmt.execute();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(pstmt!=null){try{pstmt.close();}catch(Exception e){}}
if(conn!=null){try{conn.close();}catch(Exception e){}}
}
}
/**查*/
public static User getById(Integer id){
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
User u=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123");
String sql="select * from jdbc_users where id=?";
pstmt=conn.prepareStatement(sql);//Statement创建时就是一个空的执行器
/**在execute或者executeQuery时执行死的sql语句*/
/**这只能是拼好的字符串,而不能动态的传参数,并且在数据库中每次肯定穿的是不同的sql语句,因此每次都要解析编译*/
pstmt.setInt(1, id);
rs=pstmt.executeQuery();
while(rs.next()){
u=new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
u.setPhone(rs.getString("phone"));
u.setPasswd(rs.getString("passwd"));
u.setEmail(rs.getString("email"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(pstmt!=null){try{pstmt.close();}catch(Exception e){}}
if(conn!=null){try{conn.close();}catch(Exception e){}}
}
return u;
}
}
相关推荐
sqlserver各版本驱动 DriverManager:负责加载各种不同驱动程序(Driver),并根据不同的请求,向调用者返回...PreparedStatement:用以执行包含动态参数的SQL查询和更新(在服务器端编译,允许重复执行以提高效率)
JDBC连接所有数据库步骤 1 将数据库的JDBC驱动加载到classpath中,在...5 执行SQL语句。 6 访问结果记录集ResultSet对象。 7 依次将ResultSet、Statement、PreparedStatement、Connection对象关闭,释放所占用的资源。
Java-JDBC【之】JDBC概述、获取连接、SQL注入问题与解决、查询...3.SQL注入问题与解决(Statement 、PreparedStatement) 3.1.模拟SQL注入 3.2.PreparedStatement解决 3.3.Statement 与 PreparedStatement 4.完整源码
在使用PreparedStatement对象执行SQL命令时,命令被数据库进行解析和编译,然后被放到命令缓冲区。然后,每当执行同一个PreparedStatement对象时,它就会被再解析一次,但不会被再次编译。在缓冲区中可以发现预...
PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程,下面这篇文章主要给大家介绍了关于利用JDBC的PrepareStatement打印真实SQL的方法,需要的朋友可以参考借鉴,下面来一起看看吧。
jdk1.8可使用的jdbc驱动... DriverManager:负责加载各种不同驱动程序(Driver),并根据不同的请求,向调用...PreparedStatement:用以执行包含动态参数的SQL查询和更新(在服务器端编译,允许重复执行以提高效率)
####Statement和PreparedStatement - PreparedStatement预编译的SQL执行对象 1. 可以避免SQL注入 因为在编译的时候已经把SQL的逻辑固定,不会因为替换进去的内容改变逻辑 2. 如果SQL中涉及变量 相比Statement的字符...
2.1.1 SQL数据库数据库基本操作语句 2.1.2 Microsoft SQL Server数据库设计及ODBC配置 2.1.3 Oracle 数据库的基本说明及其使用简介 2.1.4 MySQL的安装及使用简介 2.2 使用JDBC连接数据库 2.2.1 SQLJ基本知识 ...
java.sql.PreparedStatement 继承Statement接口,表示预编译的SQL语句的对象,SQL语句被预编译并且存储在PreparedStatement对象中。然后可以使用此对象高效地多次执行该语句。 java.sql.CallableStatement 用来...
获取Statement对象,通过Statement对象执行SQL语句: Statement stmt=con.createStatement(); 执行SQL查询,返回给结果集对象: ResultSet rs=stmt. executeQuery(“select * from 表名”); 或 表名”+条件); ...
JDBC(powernode CD2206)详尽版 (教学视频、源代码、SQL文件) 包含:教学视频、源代码(与博客同步)、SQL文件 一、介绍 二、JDBC常用的接口和类 2.1 Driver接口 2.2 DriverManager类 2.3 Connection接口 2.4 ...
SqlHelper.java连接数据库通用类... package org.jdbc.com.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql....
利用Statement实例通过执行静态SELECT语句完成,也可以利用PreparedStatement实例通过执行SELECT语句进行完成,还可以利用CallableStatement实例通过执行存储过程来完成。 (1)利用Statement实例通过执行静态...
JDBC JDBC(Java Database Connectivity)可以...SqlServer sqljdbc-x.jar API: 1.DriverManager 管理驱动 要先加载数据库驱动: 装载MySql驱动:Class.forName(“com.mysql.jdbc.Driver”); 装载Oracle驱动:Class.for
1、JDBC(Java Database Connection):java连接数据库统一接口API,底层主要通过直接的JDBC驱动和 JDBC-ODBC桥驱动实现与数据库的连接。 1>.JDBC驱动程序类型: <1>.JDBC-ODBC桥加ODBC驱动程序:需要ODBC驱动,适合...
jdbc(java database connectivity,java数据库连接)的api中的主要的四个类之一的java.sql.statement要求开发者付出大量的时间和精力。在使用statement获取jdbc访问时所具有的一个共通的问题是输入适当格式的日期和...
SpringMVC第一天 框架课程 1.课程计划 第一天 1、SpringMVC介绍 2、入门程序 3、SpringMVC架构讲解 a)框架结构 ...7、对sql执行结果进行解析处理 8、释放资源(resultSet、preparedstatement、connection)
一次只从数据库中查询最大maxCount条记录 ... PreparedStatement pstat = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); //最大查询到第几条记录.........
1)性能方面,PreparedStatement有预编译过程,一旦绑定SQL语句就可以执行多次,Statement执行多少次就要编译多少次SQL语句。PreparedStatement用来执行动态的SQL语句,即包含参数的SQL语句,而Statement用