idea使用连接池dbcp技术连接Oracle
1.启动oracle服务(两个)
2.在可视化Oracle数据库客户端中创建student表格
Sqlyog支持mysql但不支持oracle,可以下载其他的可视化工具,比如
需要的可以通过链接下载
链接:https://pan.baidu.com/s/1Lm38YrM4GZmk1h8Uq7MfHg 提取码:2022
这是表中的数据
3.在idea中新建一个项目,在项目下新建一个目录
将oracle的驱动包和jdbc的工具包复制进这个目录下
可以通过一下链接,获取jar包
链接:https://pan.baidu.com/s/1lZNVXCqpTZuIrP6nF4pDag 提取码:2022
4.配置jar包
点解ok
5.创建src下配置文件dbcp.properties
#配置驱动类 driverClassName=oracle.jdbc.driver.OracleDriver #配置连接字符串 url=jdbc:oracle:thin:@127.0.0.1:1521:xe #登录数据库的用户名 username=zs #登录数据库的密码 password=234 #最大活动连接数,设为0为没有限制 maxActive=50 #最大空闲连接数,设为0为没有限制 maxIdle=20 #最大等待毫秒数,设为-1为没有限制 maxWait=60000 #配置文件的键不能随便写成别的名称,因为在与源码中有对应的信息
5.创建工具类
package com.demo.util;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class ConnUtil {
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
private static DataSource ds;
private ConnUtil() {
}
static {
InputStream is = ConnUtil.class.getResourceAsStream("/dbcp.properties");
Properties properties = new Properties();
try {
properties.load(is);
ds = BasicDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
Connection conn = threadLocal.get();
if (conn == null || conn.isClosed()) {
conn = ds.getConnection();
threadLocal.set(conn);
}
return conn;
}
public static void closeConnection() {
Connection conn = threadLocal.get();
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
threadLocal.set(null);
}
}
public static void main(String[] args) throws SQLException {
System.out.println(getConnection());
}
}
6.创建类使用dbcp
(1)
package com.demo.test;
import com.demo.util.ConnUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class OracleQuery {
public static void main(String[] args) {
Connection conn = null;
try {
conn = ConnUtil.getConnection();
String sql = "select * from student";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
System.out.println("id\t\t学号\t\t姓名\t\t生日");
while(rs.next()){
System.out.println(rs.getInt("id") + "\t"
+rs.getString("sno") + "\t"
+ rs.getString("sname") + "\t"
+ rs.getTimestamp("birthday"));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
运行结果
(2) 在数据库中创建序列,实现插入数据时自增
package com.demo.test;
import com.demo.util.ConnUtil;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
public class OracleInsert {
public static void main(String[] args) {
Connection conn = null;
try {
conn = ConnUtil.getConnection();
String sql = "insert into student values(sq_student_id.nextval,?,?,null)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,"12345678");
pstmt.setString(2,"小明");
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
运行结果
|