import java.sql.*;
public class Db {
public Connection con;
public Statement st;
public PreparedStatement pst;
public CallableStatement cst;
public ResultSet rs;
public Db() {
Java连接Oracle方式
try {
// localhost:主机,服务器的名称
// 1521: 端口号
// tyl: 数据库的名称
// oracle.jdbc.driver.OracleDriver :加载数据库驱动程序
String ds = "jdbc:oracle:thin:@localhost:1521:tyl";
String user = "system";
String password = "123";
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
con = DriverManager.getConnection(ds, user, password);
// user应具有dba权限
// exp dbuser/oracle file=dbuser.dmp log=dbuser.log
// tables=table1,table2 buffer=4096000 feedback=10000
// 导入数据库 :1.Imp system/123@tyl fromuser=system touser=system
// file="d:\test.dmp"
// 2.Imp system/123@tyl fromuser=system touser=system
// file="d:\test.dmp" tables=test
// 导出数据库:1. exp system/123 file='d:\test.dmp'
// 2.exp system/123 file='d:\test.dmp' log='d:\log.txt 导出所有的
// 3.exp system/123 file='d:\test.dmp' log='d:\log.txt
// tables=test,test2 导出两张表
}
catch (Exception e) {
e.printStackTrace();
}
}
// 普通查询
public ResultSet query(String sql) {
try {
st = con.createStatement();
st.setFetchSize(2);
// st.getFetchSize();
rs = st.executeQuery(sql);
}
catch (Exception e) {
System.out.println(e.toString());
}
return rs;
}
// 添加、修改、删除
public int update(String sq) {
int x = -1;
try {
st = con.createStatement();
x = st.executeUpdate(sq);
}
catch (Exception e) {}
return x;
}
// 批处理
public void executeB(String sq[]) {
try {
con.setAutoCommit(false);
st = con.createStatement();
for (int i = 0; i < sq.length; i++) {
st.addBatch(sq[i]);
}
st.executeBatch();
con.commit();
}
catch (Exception e) {}
}
// 执行带参数的SQL语句
public void prepareQ(String s1, String s2) {
try {
String sql = "insert into test(id,name,address) values(seql.nextval,?,?)";
pst = con.prepareStatement(sql);
pst.setString(1, s1);
pst.setString(2, s2);
pst.execute();
}
catch (Exception e) {}
}
// 执行存储过程
public boolean callQ(String sq) {
boolean flag = false;
try {
cst = con.prepareCall(sq);
flag = cst.execute();
}
catch (Exception e) {}
return flag;
}
public static void main(String args[]) {
Db m = new Db();
//常用设置命令:
// set linesize=100;
// set pagesize=100; 默认80
// save e:\\test.sql
// desc test
// show user
// select user from dual
// 表空间: 创建,修改。
try {
// create tablespace asm_test datafile
// 'c:\oracle\oradata\diogenes\asm_test.dbf'size 5m EXTENT
// MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
// String str =
// " CREATE TABLESPACE tylspace DATAFILE 'tylspace.dbf' SIZE 50M EXTENT MANAGEMENT LOCAL AUTOALLOCATE";
// String str =
// "ALTER TABLESPACE temp ADD TEMPFILE 'temp_add.dbf' SIZE 500M AUTOEXTEND ON";
// String str = "drop tablespace tylspace";
// int in = m.update(str);
// System.out.println("in============" + in);
m.con.commit();
}
catch (SQLException e1) {
e1.printStackTrace();
}
// 查询:
// String queryStr = "select * from test";
// String queryStr="select * from test where id=3";
// String queryStr="select * from test where id=3 and name='name3'";
// String queryStr = "select id from test";
// 只有% ; _ ;[^] ; [] ; 4种通配符
// 以上4种通配符的含义
// % 表示零个或多个字符
// _ 表示单个字符
// \ 特殊字符
// [] 表示范围[a-f]或集合[abcdef]的任何单个字符
// [^] 表示不属于指定范围的[a-f] 或集合[abcdef]的单个字符 通常表示[^a-f] or [^abcdef]
// String queryStr = "select * from test where name like 'name1'";
// String queryStr = "select * from test where name like 'name1%'";
// String queryStr =
// "select * from test where name like 'name1_'";//无匹配
// String queryStr = "select * from test where name like 'name_'";
// 有问题,无匹配
// String queryStr =
// "select * from atm1_operatelog t where t.operatetime like to_date('2006-10-16','yyyy-MM-dd')";//
// String queryStr = "select * from test where name like '[^a-z]%'";
// select * from temp where name like 'bei%';
// 或者
// select * from temp where name like '%bei%';
// 两者的区别: 如果name建立有索引,第一种会走索引,第二种不走索引,会进行全表扫描.
// String queryStr
// ="select * from test where id in(select id from test)";
// String queryStr =
// "select * from test where exists(select null from test2 where name ='name2')";
// //如果test2存在name=name2,查询出Test中的所有值
// String queryStr =
// "select * from test t where t.currenttime like to_date('2010-08-05','yyyy-MM-dd')";
// String queryStr =
// "select * from test where name in(select name from test2)";
// String queryStr =
// "select * from test where name in(select name from test2)";
// String queryStr = "select currenttime from test "; //
// String queryStr =
// "select to_char(currenttime) from test where name in(select name from test2)";
// String queryStr = "select count(*) num from test2";
// String queryStr =
// "SELECT to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') FROM dual";
// String queryStr =
// "SELECT * FROM user_objects WHERE object_type='TABLE'";
// String queryStr =
// "SELECT * FROM user_objects WHERE object_name='TEST'";
// String queryStr = "select column_name from user_cons_columns ";
// String queryStr =
// "select column_name from user_cons_columns where constraint_name = (select u.constraint_name from user_constraints u where u.table_name = 'test' and u.constraint_type = 'P')";
// String queryStr = "select constraint_name from user_cons_columns ";
// String queryStr =
// "select constraint_name from user_cons_columns where table_name='TEST' ";
// //要用大写
// String queryStr = "select * from user_constraints ";
// String queryStr =
// "select CONSTRAINT_TYPE from user_constraints where constraint_name like 'PK_%'";
// String queryStr = "select CONSTRAINT_TYPE from user_constraints ";
// //p代表为主键
// String queryStr =
// "select CONSTRAINT_NAME from user_constraints where CONSTRAINT_TYPE = 'P'";
// //p为大写,否则不匹配
String queryStr = "select CONSTRAINT_NAME from user_constraints where CONSTRAINT_TYPE = 'R'"; // p为大写,否则不匹配
// 关于限制的类型:P 代表主键,R 代表外键,C 代表约束
ResultSet rs = m.query(queryStr);
try {
while (rs.next()) {
System.out.println(rs.getString(1));
// System.out.println(rs.getInt("id"));
// System.out.println(rs.getString("name"));
// System.out.println(rs.getInt(1));
// System.out.println(rs.getString(2));
// System.out.println(rs.getString(3));
// System.out.println(rs.getString(4));
}
}
catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
// 创建:
try {
// String newStr =
// "create table test2(id number,name varchar(100),address varchar(100))";
// String newStr =
// "alter table test2 add constraint pk_test2 primary key(id)";
// int in = m.update(newStr);
// String newStr = "alter table test add date_time date";
// String newStr = "update test set date_time =(select sysdate from dual) where name='name2'";
String newStr = "create or replace function testfun(param in varchar)" +
" return number" +
" as returnvalue number ;" +
"begin" +
" select count(*) into returnvalue from test where name=param;" +
"return returnvalue;" +
"end;";
int in = m.update(newStr);
// EN_DATE datetime
System.out.println("in============" + in);
m.con.commit();
}
catch (SQLException e1) {
e1.printStackTrace();
}
// 插入:
try {
// String insertStr
// ="insert into test values(seql.nextval,'name1','shaanxi','112223')";
// String insertStr =
// "insert into test(id,name) values(seql.nextval,'name2')";
String insertStr = "create view viewtest1 as select t1.id,t1.name from test t1,test2 t2";
// String insertStr = "drop view viewtest";
// int in = m.update(insertStr);
// System.out.println("in============" + in);
m.con.commit();
}
catch (SQLException e1) {
e1.printStackTrace();
}
// 修改
try {
// String updateStr = "update test set name='name3' where id=3";
// String updateStr="alter table test add address varchar(100)";
// String updateStr="alter table test add currenttime date";
// String updateStr =
// "alter table test add constraint pk_test primary key(id)";
// String updateStr =
// "alter table test add constraint pk_test primary key(id)";
// String updateStr
// ="alter table test2 add constraint pk_test primary key(id)";
// String updateStr =
// "update test set currenttime=sysdate where id=14";
// 修改所有
// String updateStr = "update test set currenttime=sysdate";
// String updateStr = "insert into test2(id) values(seql.nextval)";
// String updateStr =
// "update test set forid=(select count(*) from test2) where name='name1'";
// String updateStr = "alter table test add forid number";
// String updateStr =
// "alter table test add constraint fk_test foreign key (forid) references test2(id)";
// String updateStr = "alter table test add pho varchar(100)";
// String updateStr = "alter table test rename column pho to phone";
// String updateStr = "delete from test where name='name3'";
// String updateStr = "delete from test";
// String updateStr = "alter table test add check (name!='name')";
// String updateStr =
// "insert into test(id,name) values(seql.nextval,'name')"; //无法插入
// String updateStr = " create synonym syn for testsyn";
String updateStr = " drop synonym syn";
// String updateStr = "alter table test add check (name!='name')";
// int in = m.update(updateStr);
// System.out.println("in============" + in);
m.con.commit();
}
catch (SQLException e1) {
e1.printStackTrace();
}
// 调用存储过程
/*
* boolean x=m.callQ("{call ww}"); if(x==true){
* System.out.println("ok"); }else{ System.out.println("no"); }
*/
// 执行带参数的语句
// m.prepareQ("pp","458");
// 批处理
try {
String st1 = "insert into test(id,name) values(seql.nextval,'name3')";
String st2 = "delete from test where id=32";
String st[] = { st1, st2 };
// m.executeB(st);
}
catch (Exception e1) {
e1.printStackTrace();
}
}
}