java連線Oracle並實用儲存過程實現增刪改查(CRUD)
阿新 • • 發佈:2019-02-15
public class User_oracle {
//增
public void insert_exp_user(String username,String password){
Connection con=DBUtil.getConnection();
CallableStatement cs=null;
ResultSet rs=null;
try {
String sql="{call insert_exp_user(?,?)}";
cs=con.prepareCall(sql);
cs.setString(1, username);
cs.setString(2, password);
cs.execute();
} catch (Exception e) {
throw new RuntimeException(e);
}finally{
DBUtil.close(con, cs, rs);
}
}
//改
public void update_exp_user(String username,String password){
Connection con=DBUtil.getConnection();
CallableStatement cs=null;
try {
String sql="{call update_exp_user(?,?)}";
cs=con.prepareCall(sql);
cs.setString(1, username);
cs.setString(2, password);
cs.execute();
} catch (Exception e) {
throw new RuntimeException(e);
}finally{
DBUtil.close(con, cs);
}
}
//刪
public void delete_exp_user(String username){
Connection con=DBUtil.getConnection();
CallableStatement cs=null;
ResultSet rs=null;
try {
String sql="{call delete_exp_user(?)}";
cs=con.prepareCall(sql);
cs.setString(1, username);
cs.execute();
} catch (Exception e) {
throw new RuntimeException(e);
}finally{
DBUtil.close(con, cs, rs);
}
}
//查詢單個數據
public Exp_user select_one_exp_user(int id){
Connection con=DBUtil.getConnection();
CallableStatement cs=null;
ResultSet rs=null;
Exp_user exp_user=null;
try {
String sql="{call select_one_exp_user(?,?,?)}";
cs=con.prepareCall(sql);
cs.setInt(1, id);
cs.registerOutParameter(2, Types.VARCHAR);
cs.registerOutParameter(3, Types.VARCHAR);
cs.execute();
exp_user=new Exp_user(id, cs.getString(2), cs.getString(3));
} catch (Exception e) {
throw new RuntimeException(e);
}finally{
DBUtil.close(con, cs, rs);
}
return exp_user;
}
//查詢所有
public ArrayList<Exp_user> selec_all_exp_user(){
ArrayList<Exp_user> exp_users =new ArrayList<>();
Connection con=DBUtil.getConnection();
CallableStatement cs=null;
ResultSet rs=null;
try {
cs = con.prepareCall("{call mypack.myproc(?)}");
cs.registerOutParameter(1, OracleTypes.CURSOR);
cs.execute();
rs = (ResultSet)cs.getObject(1);
while(rs!=null&& rs.next()){
exp_users.add(new Exp_user(rs.getInt("id"), rs.getString("username"), rs.getString("password")));
}
} catch (Exception e) {
throw new RuntimeException(e);
}finally{
DBUtil.close(con, cs, rs);
}
return exp_users;
}
public static void main(String[] args) {
User_oracle u=new User_oracle();
//u.insert_exp_user("jack", "123");
//u.insert_exp_user("lucy", "123");
//u.insert_exp_user("alex", "123");
//u.update_exp_user("tom", "111");
//u.delete_exp_user("tom");
//System.out.println(u.select_one_exp_user(3).toString());
System.out.println(u.selec_all_exp_user().toString());
}
}
/*
建立序列
create sequence seq_exp_user
建立表:
create table exp_user(id number,username varchar2(50),password varchar2(50))
增加儲存過程:
reate or replace procedure update_exp_user(
param1 varchar2,param2 varchar2
)as
begin
insert into exp_user values(seq_exp_user.nextval,param1,param1);
commit;
end;
更新儲存過程:
reate or replace procedure update_exp_user(
param1 varchar2,param2 varchar2
)as
begin
update exp_user set password = param2 where username = param1;
commit;
end;
刪除儲存過程:
create or replace procedure delete_exp_user(
param varchar2
)as
begin
delete from exp_user where username=param;
commit;
end;
查詢一條儲存過程:
create or replace procedure select_one_exp_user(
param1 in number,param2 out varchar2,param3 out varchar2
)as begin
select username into param2 from exp_user where id = param1;
select password into param3 from exp_user where id = param1;
//--select username,password into param2,param3 from exp_user where id = param1;
end;
查詢多條(1):這裡建了一個包,其中有兩個元素:mycursor遊標和myproc儲存過程.
CREATE OR REPLACE PACKAGE mypack IS
TYPE mycursor IS REF CURSOR;
PROCEDURE myproc(outcursor IN OUT mycursor);
END mypack;
查詢多條(2):這裡詳細定義了mycursor和myproc的body。注意:CREATE PACKAGE和CREATE PACKAGE BODY不能一起執行,必須先後執行,否則會報錯.
CREATE OR REPLACE PACKAGE BODY mypack IS
PROCEDURE myproc(
outcursor IN OUT mycursor
)
IS
BEGIN
OPEN outcursor FOR
SELECT * FROM Exp_user;
RETURN;
END myproc;
END;
*/
//增
public void insert_exp_user(String username,String password){
Connection con=DBUtil.getConnection();
CallableStatement cs=null;
ResultSet rs=null;
try {
String sql="{call insert_exp_user(?,?)}";
cs=con.prepareCall(sql);
cs.setString(1, username);
cs.setString(2, password);
cs.execute();
} catch (Exception e) {
throw new RuntimeException(e);
}finally{
DBUtil.close(con, cs, rs);
}
}
//改
public void update_exp_user(String username,String password){
Connection con=DBUtil.getConnection();
CallableStatement cs=null;
try {
String sql="{call update_exp_user(?,?)}";
cs=con.prepareCall(sql);
cs.setString(1, username);
cs.setString(2, password);
cs.execute();
} catch (Exception e) {
throw new RuntimeException(e);
}finally{
DBUtil.close(con, cs);
}
}
//刪
public void delete_exp_user(String username){
Connection con=DBUtil.getConnection();
CallableStatement cs=null;
ResultSet rs=null;
try {
String sql="{call delete_exp_user(?)}";
cs=con.prepareCall(sql);
cs.setString(1, username);
cs.execute();
} catch (Exception e) {
throw new RuntimeException(e);
}finally{
DBUtil.close(con, cs, rs);
}
}
//查詢單個數據
public Exp_user select_one_exp_user(int id){
Connection con=DBUtil.getConnection();
CallableStatement cs=null;
ResultSet rs=null;
Exp_user exp_user=null;
try {
String sql="{call select_one_exp_user(?,?,?)}";
cs=con.prepareCall(sql);
cs.setInt(1, id);
cs.registerOutParameter(2, Types.VARCHAR);
cs.registerOutParameter(3, Types.VARCHAR);
cs.execute();
exp_user=new Exp_user(id, cs.getString(2), cs.getString(3));
} catch (Exception e) {
throw new RuntimeException(e);
}finally{
DBUtil.close(con, cs, rs);
}
return exp_user;
}
//查詢所有
public ArrayList<Exp_user> selec_all_exp_user(){
ArrayList<Exp_user> exp_users =new ArrayList<>();
Connection con=DBUtil.getConnection();
CallableStatement cs=null;
ResultSet rs=null;
try {
cs = con.prepareCall("{call mypack.myproc(?)}");
cs.registerOutParameter(1, OracleTypes.CURSOR);
cs.execute();
rs = (ResultSet)cs.getObject(1);
while(rs!=null&& rs.next()){
exp_users.add(new Exp_user(rs.getInt("id"), rs.getString("username"), rs.getString("password")));
}
} catch (Exception e) {
throw new RuntimeException(e);
}finally{
DBUtil.close(con, cs, rs);
}
return exp_users;
}
public static void main(String[] args) {
User_oracle u=new User_oracle();
//u.insert_exp_user("jack", "123");
//u.insert_exp_user("lucy", "123");
//u.insert_exp_user("alex", "123");
//u.update_exp_user("tom", "111");
//u.delete_exp_user("tom");
//System.out.println(u.select_one_exp_user(3).toString());
System.out.println(u.selec_all_exp_user().toString());
}
}
/*
建立序列
create sequence seq_exp_user
建立表:
create table exp_user(id number,username varchar2(50),password varchar2(50))
增加儲存過程:
reate or replace procedure update_exp_user(
param1 varchar2,param2 varchar2
)as
begin
insert into exp_user values(seq_exp_user.nextval,param1,param1);
commit;
end;
更新儲存過程:
reate or replace procedure update_exp_user(
param1 varchar2,param2 varchar2
)as
begin
update exp_user set password = param2 where username = param1;
commit;
end;
刪除儲存過程:
create or replace procedure delete_exp_user(
param varchar2
)as
begin
delete from exp_user where username=param;
commit;
end;
查詢一條儲存過程:
create or replace procedure select_one_exp_user(
param1 in number,param2 out varchar2,param3 out varchar2
)as begin
select username into param2 from exp_user where id = param1;
select password into param3 from exp_user where id = param1;
//--select username,password into param2,param3 from exp_user where id = param1;
end;
查詢多條(1):這裡建了一個包,其中有兩個元素:mycursor遊標和myproc儲存過程.
CREATE OR REPLACE PACKAGE mypack IS
TYPE mycursor IS REF CURSOR;
PROCEDURE myproc(outcursor IN OUT mycursor);
END mypack;
查詢多條(2):這裡詳細定義了mycursor和myproc的body。注意:CREATE PACKAGE和CREATE PACKAGE BODY不能一起執行,必須先後執行,否則會報錯.
CREATE OR REPLACE PACKAGE BODY mypack IS
PROCEDURE myproc(
outcursor IN OUT mycursor
)
IS
BEGIN
OPEN outcursor FOR
SELECT * FROM Exp_user;
RETURN;
END myproc;
END;
*/