package com.enhance.jdbc;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class PreparedStatementTest {
private String driver;
private String url;
private String user;
private String pass;
private Statement stmt;
private ResultSet rs;
private PreparedStatement pstmt;
public void initParam(String paramFile) throws Exception{
Properties prop=new Properties();
prop.load(new FileInputStream(paramFile));
driver=prop.getProperty("driver");
url=prop.getProperty("url");
user=prop.getProperty("user");
pass=prop.getProperty("pass");
}
public Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName(driver);
return DriverManager.getConnection(url,user,pass);
}
public void executeDmlAndDdlUseStmt()throws Exception{
long start=System.currentTimeMillis();
Connection conn=null;
try {
conn=getConnection();
stmt=conn.createStatement();
for (int i = 0; i < 1000; i++) {
stmt.executeUpdate("insert into my_test (test_name)"
+ " values('姓名"+i+"')");
}
System.out.println("使用Statement费时:"+(System.currentTimeMillis()-start));
}finally{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
}
public void executeDmlAndDdlUsePstmt()throws Exception{
long start=System.currentTimeMillis();
Connection conn=null;
try {
conn=getConnection();
pstmt=conn.prepareStatement("insert into my_test (test_name) values(?)");
for (int i = 0; i < 1000; i++) {
pstmt.setString(1, "姓名2"+i);
pstmt.executeUpdate();
}
System.out.println("使用PreparedStatement费时:"+(System.currentTimeMillis()-start));
}finally{
if(rs!=null)
rs.close();
if(pstmt!=null)
pstmt.close();
if(conn!=null)
conn.close();
}
}
/**
* 创建存储过程
* delimiter //
* create or replace procedure(a int,b int,out sum int)
* begin
* set sum=a+b;
* end;
* //
*/
public void callProcedure() throws Exception{
try {
Class.forName(driver);
conn=DriverManager.getConnection(url,user,pass);
cstmt=conn.prepareCall("{call add_proc(?,?,?)}");
cstmt.setInt(1, 4);
cstmt.setInt(2, 5);
//注册 输出参数
cstmt.registerOutParameter(3, Types.INTEGER);
cstmt.execute();
System.out.println("执行结果是:"+cstmt.getInt(3));
}finally{
if(cstmt!=null)
cstmt.close();
if(conn!=null)
conn.close();
}
}
public static void main(String[] args) throws Exception{
PreparedStatementTest pst=new PreparedStatementTest();
pst.initParam("src/mysql.ini");
pst.executeDmlAndDdlUseStmt();
pst.executeDmlAndDdlUsePstmt();
}
}
分享到:
相关推荐
Statement和PreparedStatement之间的区别
java中PreparedStatement和Statement的区别
主要介绍了详解Java的JDBC中Statement与PreparedStatement对象,PreparedStatement一般来说比使用Statement效率更高,需要的朋友可以参考下
首先我们设计Connection接口的代理类_Connection,这个代理类接管了Connection接口中所有可能获取到Statement或者PreparedStatement接口实例的方法,例如:prepareStatement和createStatement。改变这两个方法使之...
Java-JDBC【之】JDBC概述、获取连接、SQL注入问题与解决、查询...3.SQL注入问题与解决(Statement 、PreparedStatement) 3.1.模拟SQL注入 3.2.PreparedStatement解决 3.3.Statement 与 PreparedStatement 4.完整源码
NULL 博文链接:https://stevenjohn.iteye.com/blog/968877
什么时候用statement,什么时候用preparedstatement
jdbc中preparedStatement比Statement的好处
主要介绍了java中PreparedStatement和Statement详细讲解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
jdbc2.0版 PreparedStatement接口的用法
NULL 博文链接:https://huiminchen.iteye.com/blog/1097332
NULL 博文链接:https://chaoyi.iteye.com/blog/2088080
Java面试题34.jdbc中preparedStatement比Statement的好处.mp4
4 建立Statement对象或PreparedStatement对象。 5 执行SQL语句。 6 访问结果记录集ResultSet对象。 7 依次将ResultSet、Statement、PreparedStatement、Connection对象关闭,释放所占用的资源。
Request对象 Servle_jsp Servlet执行方法 Statement和PreparedStatement 的区别
JDBC中PreparedStatement接口提供的execute、executeQuery和executeUpdate之间的区别及用法
关于PreparedStatement插入Date类型值的方法.txt
PreparedStatement详细用法
####Statement和PreparedStatement - PreparedStatement预编译的SQL执行对象 1. 可以避免SQL注入 因为在编译的时候已经把SQL的逻辑固定,不会因为替换进去的内容改变逻辑 2. 如果SQL中涉及变量 相比Statement的字符...
jdbc数据软件测试中使用JDBC插入大量数据的性能测试使用jdbc向数据库插入100000条记录,分别使用statement,PreparedStatement,及PreparedStatement+批处理3种方式进行测试:1、使用statement插入100000条记录...