JDBC PreparedStatement ,CallableStatement,以及事务,回滚举例

程序中用到的类,文件,jar

代码:

 

1,文件:db.properties文件内容

user=root
password=123
url=jdbc:mysql:///student_db
driver=com.mysql.jdbc.Driveraaa

 

2,类Utils.class

 

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class Utils {

private static Properties properties=null;
static
{
properties=new Properties();
try(InputStream in=Utils.class.getResourceAsStream("/db.properties");){
properties.load(in);
String driver=properties.getProperty("driver");
//在这里加载驱动,防止多次加载
Class.forName(driver);

}
catch(Exception e){
}
}

public static Connection getConnection()
{
String url=properties.getProperty("url");
String user=properties.getProperty("user");
String password=properties.getProperty("password");
try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return null;

}
//这里可以用来关闭资源,通过传参
public static void close()
{
//具体代码,这里不再写,但注意,谁先初始化,谁最后关闭,意思就是倒着关
}
}

 

3,类JdbcDome,主要用于测试存储过程,和函数

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;

import com.jdbc.utils.Utils;

public class JdbcDome {

public static void main(String[] args) {
// TODO 自动生成的方法存根

Connection con=null;
CallableStatement call=null;

try {
con=Utils.getConnection();
//下面测试下事务,回滚
con.setAutoCommit(false);
String sql="{call tea_pro(?,?,?)}";//存储过程
call=con.prepareCall(sql);
call.setInt(1,1);
call.setInt(2,2);
call.registerOutParameter(3,Types.CHAR);
call.execute();
System.out.println(call.getString(3));//以 Java 编程语言中 String 的形式获取 JDBC


//函数
sql="{?=call teache_info2(?,?)}";
call=con.prepareCall(sql);
call.registerOutParameter(1, Types.CHAR);
call.setInt(2, 1);
call.setInt(3, 1);
call.execute();
System.out.println(call.getString(1));
//CHAR、VARCHAR 或 LONGVARCHAR 参数的值。

con.commit();
con.rollback();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

finally{

Utils.close();

}
}

}

 

4,本类用于测试PreparedStatement,事务等,为了使程序变得简单,这里又开了一个主函数进行测试

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.jdbc.utils.Utils;

public class SessionDome {

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成的方法存根


Connection con=null;
PreparedStatement statement=null;

try {
//从Utils获取连接
con=Utils.getConnection();
//打开事务
con.setAutoCommit(false);
//(1002,'李四','女','1970-01-21','北京市海淀区'),
statement=con.prepareStatement("insert into teacher values(?,?,?,?,?,?)");
statement.setInt(1, 37);
statement.setInt(2, 2111);
statement.setString(3, "李六");
statement.setString(4, "男");
statement.setDate(5, new Date(2001,1,1));
statement.setString(6, "中国");
statement.executeUpdate();

statement=con.prepareStatement("delete from teacher where num=1003");
statement.executeUpdate();
con.rollback();//回滚
con.commit();//提交事务

} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally{

Utils.close();

}

}

}

 

转载于:https://www.cnblogs.com/jiuzheyange/p/4743277.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值