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();

}

}

}

 

posted @ 2015-08-19 20:13  就这样e  阅读(1153)  评论(0编辑  收藏  举报