04.JDBC编程之指定变量&批处理,04.jdbc编程变量
转载请标明出处:http://blog.csdn.net/u012637501一、指定SQL语句中的变量 1.PreparedStatement接口 PreparedStatement接口继承Statement, PreparedStatement 实例包含已编译的 SQL 语句,所以其执行速度要快于 Statement 对象。 包含于 PreparedStatement 对象中的 SQL 语句可具有一个或多个 IN 参数。IN参数的值在 SQL 语句创建时未被指定。相反的,该语句为每个 IN 参数保留一个问号(“?”)作为占位符。每个问号的值必须在该语句执行之前,通过适当的setXXX 方法来提供。另外,作为 Statement 的子类,PreparedStatement 继承了 Statement 的所有功能。三种方法 execute、 executeQuery 和 executeUpdate 已被更改以使之不再需要参数。 由于 PreparedStatement 对象已预编译过,所以其执行速度要快于 Statement 对象。因此,多次执行的 SQL 语句经常创建为 PreparedStatement 对象,以提高效率。为此,在JDBC应用中应该始终以PreparedStatement代替Statement,即尽量不要使用Statement。 Interface PreparedStatement(java.sql)
boolean |
execute()
Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement.
|
ResultSet |
executeQuery()
Executes the SQL query in this PreparedStatement object and returns the ResultSet object
generated by the query.
|
int |
executeUpdate()
Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE;
or an SQL statement that returns nothing, such as a DDL statement.
|
void |
setDouble(int parameterIndex,
double x)
Sets the designated parameter to the given Java double value.
|
void |
setFloat(int parameterIndex,
float x)
Sets the designated parameter to the given Java float value.
|
void |
setInt(int parameterIndex,
int x)
Sets the designated parameter to the given Java int value.
|
void |
setLong(int parameterIndex,
long x)
Sets the designated parameter to the given Java long value.
|
void |
setNull(int parameterIndex,
int sqlType)
Sets the designated parameter to SQL NULL.
|
void |
setShort(int parameterIndex,
short x)
Sets the designated parameter to the given Java short value.
|
void |
setSQLXML(int parameterIndex, SQLXML xmlObject)
Sets the designated parameter to the given java.sql.SQLXML object.
|
void |
setString(int parameterIndex, String x)
Sets the designated parameter to the given Java String value.
|
void |
setURL(int parameterIndex, URL x)
Sets the designated parameter to the given java.net.URL value
|
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- /*MySQL数据库编程
- * 实例(4):使用PreparedStatement灵活指定SQL语句中的变量*/
- public class JDBC_PreparedStatement {
- public static void main(String[] args) {
- if(args.length!=3) //输入不正确,非正常退出
- {
- System.out.println( "Parament Error,Please Input Again!");
- System.exit(-1);
- }
- String nameParam=args[0]; //获取命令行第一个参数
- int ageParam=Integer.parseInt(args[1]); //获取命令行第二个参数,并转换为整型
- int scoreParam=Integer.parseInt(args[2]);//获取命令行第三个参数,并转换为整型
- //0.连接数据库相关参数
- String url="jdbc:mysql://localhost:3306/jdbc_test_db"; //数据库URL(资源定位唯一标识符)
- String DBusername="root"; //数据库用户名
- String DBpasswd="111111"; //数据库密码
- //1.加载数据库驱动,将Driver注册到DriverManager中
- try{
- Class.forName("com.mysql.jdbc.Driver");
- }catch(ClassNotFoundException e){
- e.printStackTrace();
- }
- //2.通过数据库URL连接到数据库
- Connection conn=null;
- PreparedStatement prestmt=null;
- try{
- conn=DriverManager.getConnection(url, DBusername, DBpasswd);
- //3.获取PreparedStatement对象
- String sql="insert into test(name,age,score) values(?,?,?)";
- prestmt=conn.prepareStatement(sql);
- prestmt.setString(1, nameParam); //分别给变量设值
- prestmt.setInt(2, ageParam);
- prestmt.setInt(3, scoreParam);
- prestmt.execute();
- }catch(SQLException e){
- e.printStackTrace();
- }
- //5.释放JDBC资源
- if(prestmt!=null) //关闭声明
- {
- try{
- prestmt.close();
- }catch(SQLException e){
- e.printStackTrace();
- }
- }
- if(conn!=null) //关闭连接
- {
- try{
- conn.close();
- }catch(SQLException e){
- e.printStackTrace();
- }
- }
- }
- }
(2)运行结果

二、批处理SQL语句 (1)源代码
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- /*MySQL数据库编程
- * 实例(5):JDBC批量处理DML语句.分别尝试Statement、PreparedStatement*/
- public class JDBC_Batch {
- public static void main(String[] args) {
- //0.连接数据库相关参数
- String url="jdbc:mysql://localhost:3306/jdbc_test_db"; //数据库URL(资源定位唯一标识符)
- String DBusername="root"; //数据库用户名
- String DBpasswd="111111"; //数据库密码
- //1.加载数据库驱动,将Driver注册到DriverManager中
- try{
- Class.forName("com.mysql.jdbc.Driver");
- }catch(ClassNotFoundException e){
- e.printStackTrace();
- }
- //2.通过数据库URL连接到数据库
- Connection conn=null;
- PreparedStatement prestmt=null;
- try{
- conn=DriverManager.getConnection(url, DBusername, DBpasswd);
- //3.获取PreparedStatement对象
- String sql="insert into test(name,age,score) values(?,?,?)";
- prestmt=conn.prepareStatement(sql);
- prestmt.setString(1,"aaa");
- prestmt.setInt(2, 19);
- prestmt.setInt(3, 55);
- prestmt.execute(); //a.添加第一条插入记录
- prestmt.setString(1,"bbb");
- prestmt.setInt(2, 20);
- prestmt.setInt(3, 66);
- prestmt.execute(); //a.添加第二条插入记录
- prestmt.setString(1,"ccc");
- prestmt.setInt(2, 21);
- prestmt.setInt(3, 77);
- prestmt.execute(); //a.添加第三条插入记录
- }catch(SQLException e){
- e.printStackTrace();
- }
- //5.释放JDBC资源
- if(prestmt!=null) //关闭声明
- {
- try{
- prestmt.close();
- }catch(SQLException e){
- e.printStackTrace();
- }
- }
- if(conn!=null) //关闭连接
- {
- try{
- conn.close();
- }catch(SQLException e){
- e.printStackTrace();
- }
- }
- }
- }
说明分析:
这里使用的是SQL语句静态插入数据,我们也可以使用Statement的addBatch方法实现JDBC批量处理SQL语句
Statement stmt=conn.createStatement();
stmt.addBatch("insert into test(name,age,score) values('ppp',30,88)");
stmt.addBatch("insert into test(name,age,score) values('ooo',31,89)");
stmt.addBatch("insert into test(name,age,score) values('qqq',32,90)");
stmt.executeBatch(); //批量执行SQL语句
参考资料: http://blog.sina.com.cn/s/blog_77eba18f01019csh.html http://docs.oracle.com/javase/8/docs/api/index.html
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。