1、线程池进行数据库查询-Java实现
调用方法
public static void main(String[] args)throws Exception {
Pool.connectDateBase();
}
创建线程池
public static BasicDataSource createConnectionPool () throws Exception {
BasicDataSource bds = new BasicDataSource(); //直接new连接池
bds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
bds.setUrl(url);//连哪里
bds.setUsername("scott");//用户名
bds.setPassword("tiger");//密码
bds.setInitialSize(3);//启动连接池后,初始的连接数
bds.setMaxActive(100);//最大活动连接数
bds.setMaxIdle(30);//最大空闲连接数
bds.setMaxWait(10000);//最长等候时间,过时就连接失败
bds.setMinIdle(1);//最小连接数
return bds;
}
连接服务的方法
public static void connectDateBase()throws Exception {
BasicDataSource bds = Pool.createConnectionPool();
//连接池的属性一般都是由项目经理在配置文件中设置好
Connection conn = bds.getConnection();
Statement st = conn.createStatement();
String sql = "select * from emp";
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("empno");
String name = rs.getString(2);
double sal = rs.getDouble("sal");
System.out.println(id+","+name+","+sal);
}
rs.close();
st.close();
conn.close();
}
2、存储过程学习-Java实现
数据库创建
drop table users;
create table users(
u_id number(37) primary key,
uname varchar2(200) unique not null ,
upass varchar2(200) not null
);
drop sequence user_squ;
create sequence user_squ;
存储过程创建
drop procedure getMax;
create or replace procedure getMax
is
begin
insert into users values (user_squ.nextval,user_squ.nextval,'123||user_squ.currval');
dbms_output.put_line('3123312');
end;
方法调用
public static void main(String[] args) throws Exception {
//Login.commitSignal();
//Login.commitSome();
//Login.commitMyself();
Login.commitProduce();
}
JDBC插入SQL语句插入数据库
"insert into student values (null,'"+name+"','"+value+"')"单括号里面先写两个",里面再写两个+,里面再写变量名
PrepareStatement进行setString的时候,只能是数字,不能像Statement的随意拼接了
错误示例: preparedStatement.setString(1, "user_squ.nextval");
删除存储过程
drop procedure 名
清除表的数据
truncate table 名
创建链接服务的方法
public static Connection connectDateBase(Properties properties)throws Exception {
FileInputStream inputStream = new FileInputStream("./src/db.properties");
properties.load(inputStream);
inputStream.close();
String url = properties.getProperty("url");
String username = properties.getProperty("username");
String passwd = properties.getProperty("passwd");
Connection conn = DriverManager.getConnection(url, username, passwd);
return conn;
}
单个数据提交
public static void commitSignal() throws Exception{
Properties properties = new Properties();
Connection connection = Login.connectDateBase(properties);
String sql = "insert into users values (user_squ.nextval,?,'123||user_squ.currval')";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, "小青");
preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
}
多个数据提交
public static void commitSome() throws Exception{
Properties properties = new Properties();
Connection connection = Login.connectDateBase(properties);
String sql = "insert into users values (user_squ.nextval,'123||user_squ.currval',?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < 10000; i++) {
preparedStatement.setString(1, "小青");
preparedStatement.addBatch();
if (i%100==99) preparedStatement.executeBatch();
}
preparedStatement.close();
connection.close();
}
设置手动提交
public static void commitMyself() throws Exception{
Properties properties = new Properties();
Connection connection = Login.connectDateBase(properties);
connection.setAutoCommit(false);
String sql = "insert into users values (user_squ.nextval,?,'123||user_squ.currval')";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, "小青");
preparedStatement.executeUpdate();
preparedStatement.close();
connection.commit();
connection.close();
}
提交事务
public static void commitProduce() throws Exception{
Properties properties = new Properties();
Connection connection = Login.connectDateBase(properties);
String sql = "call getMax()";
CallableStatement callableStatement = connection.prepareCall(sql);
for (int i = 0; i < 200; i++) {
callableStatement.execute();
}
callableStatement.close();
connection.close();
}
3、properties使用-Java实现
基础使用
properities 属性设置
url=jdbc:oracle:thin:@127.0.0.1:1521:XE
username=scott
passwd=tiger
简版JDBC连接数据库,使用DML
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// 先明确我们要干什么?
// 1.数据库管理
// 2.数据库连接
// 3.建立执行平台
// 4.执行SQL语句
// 5.平台关闭
//1.
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.
String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
Connection conn = DriverManager.getConnection(url, "scott", "tiger");
//3.
Statement st = (Statement) conn.createStatement();
String sql ="insert into dept values (88,'NetWork','BOSTON')";
//4.
st.executeUpdate(sql);
//5.
st.close();
conn.close();
}
简版JDBC连接数据库,使用Select
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// 先明确我们要干什么?
// 1.数据库管理
// 2.数据库连接
// 3.建立执行平台
// 4.执行SQL语句
// 5.平台关闭
//1.
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.
String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
Connection conn = DriverManager.getConnection(url, "scott", "tiger");
//3.
Statement st = (Statement) conn.createStatement();
String sql ="select * from dept";
st.executeQuery(sql);
//4.
ResultSet resultSet = st.getResultSet();
while (resultSet.next()) {
String num = resultSet.getString(1);
String work = resultSet.getString(2);
String loc = resultSet.getString("loc");
System.out.println(num+","+work+","+loc);
}
resultSet.close();
st.close();
conn.close();
}
简版JDBC连接数据库,无冲突版使用Select
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// 先明确我们要干什么?
// 1.数据库管理
// 2.数据库连接
// 3.建立执行平台
// 4.执行SQL语句
// 5.平台关闭
//1.
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.
String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
Connection conn = DriverManager.getConnection(url, "scott", "tiger");
//3.
Statement st = (Statement) conn.createStatement();
String sql ="select * from emp";
st.executeQuery(sql);
//4.
ResultSet resultSet = st.getResultSet();
ResultSetMetaData date = resultSet.getMetaData();
int count = date.getColumnCount();
while (resultSet.next()) {
for (int i = 1; i <= count; i++) {
String string = date.getColumnName(i);
String value = resultSet.getString(i);
System.out.print(string+","+value+" ");
}
System.out.println();
}
//5.
resultSet.close();
st.close();
conn.close();
}
简版JDBC连接数据库,进行项目分级,配置文件路径(properties)
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// 先明确我们要干什么?
// 1.找路径
// 2.取得属性
// 3.数据库管理
// 4.数据库连接
// 5.建立执行平台
// 6.执行SQL语句
// 7.平台关闭
//1.
File file =new File(".");
System.out.print(file.getAbsolutePath());
//2.
Properties properties = new Properties();
FileInputStream inputStream = new FileInputStream("./src/db.properties");
properties.load(inputStream);
inputStream.close();
String url = properties.getProperty("url");
String username = properties.getProperty("username");
String passwd = properties.getProperty("passwd");
//3.
Connection conn = DriverManager.getConnection(url, username, passwd);
//4.
Statement st = (Statement) conn.createStatement();
//5.
String sql ="select * from emp";
st.executeQuery(sql);
//6.
ResultSet resultSet = st.getResultSet();
ResultSetMetaData date = resultSet.getMetaData();
int count = date.getColumnCount();
while (resultSet.next()) {
for (int i = 1; i <= count; i++) {
String string = date.getColumnName(i);
String value = resultSet.getString(i);
System.out.print(string+","+value+" ");
}
System.out.println();
}
//7.
resultSet.close();
st.close();
conn.close();
}