创建基本数据访问类
public class BaseDao {
static {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static java.sql.Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:oracle:thin:@192.168.60.10:1521:orcl", "wateruser", "orcl");
}
public static void closeAll(java.sql.ResultSet rs, java.sql.Statement statement, java.sql.Connection connection) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
if (statement != null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
业主表实体类
public class Owners {
private Long id;
private String name;
private Long address;
private String housenumber;
private String watermeter;
private java.util.Date adddate;
private Long ownertypeid;
JDBC调用存储过程
public class OwnersProDao {
public static long add(Owners owners){
long id = 0;
java.sql.Connection conn = null;
java.sql.CallableStatement stmt=null;
try {
conn=BaseDao.getConnection();
stmt=conn.prepareCall("{call pro_owners_add(?,?,?,?,?,?)}");
stmt.setString(1, owners.getName());
stmt.setLong(2,owners.getAddress());
stmt.setString(3,owners.getHousenumber());
stmt.setString(4,owners.getWatermeter());
stmt.setLong(5,owners.getOwnertypeid());
stmt.registerOutParameter(6, OracleTypes.NUMBER);
stmt.execute();
id=stmt.getLong(6);
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally{
BaseDao.closeAll(null,stmt,conn);
}
return id;
}
}
测试JDBC调用存储过程
public class Test4 {
public static void main(String[] args) {
Owners owners=new Owners();
owners.setName("姜子牙");
owners.setAddress(2L);
owners.setHousenumber("2-1234");
owners.setOwnertypeid(1L);
owners.setWatermeter("333456");
long add = OwnersProDao.add(owners);
System.out.println(add);
}
}