实现JDBC基础步骤:
首先在项目里放入ojdbc6.jar架包,把架包放进lib文件夹里,右键架包点Add as Library...
如图 1 就是配置好的架包
2 配置文件从配置文件中读取数据,获取数据库的地址,用户名,密码,等于做一个封装
实现JDBC基础步骤:
1.加载驱动(选择要连接的数据库)
2.获取连接(与数据库建立连接)
3.准备数据,sql
4.封装处理块
5.发送->接收结果集
6.处理结果
7.关闭
public class JDBCDemo01 {
public static void main(String[] args) throws Exception {
//1.加载驱动(选择要连接的数据库) oracle.jdbc.driver.OracleDriver
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.获取连接(与数据库建立连接)
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","SCOTT","TIGER");
//3.准备数据,sql
String sql = "select * from dept"; //切记sql后不要添加;结尾
//4.封装处理块
Statement state = conn.createStatement(); //静态处理块
//5.发送->接收结果集
ResultSet result = state.executeQuery(sql);
//6.处理结果
while(result.next()){
int deptno = result.getInt(1);
String dname = result.getString(2);
String loc = result.getString(3);
System.out.println(deptno +"-->" +dname +"-->" +loc);
}
//7.关闭
result.close();
state.close();
conn.close();
}
}
优化JDBC:
优化JDBC基础流程: 异常捕获 通过配置文件实现软编码
配置文件(db.properties):
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:XE
username=SCOTT
password=TIGER
public class JDBCDemo02 {
public static void main(String[] args){
//properties 从配置文件中读取数据
Properties pro = new Properties();
try {
pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
} catch (IOException e) {
e.printStackTrace();
}
//1.加载驱动(选择要连接的数据库) oracle.jdbc.driver.OracleDriver
try {
Class.forName(pro.getProperty("driver"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = null;
Statement state = null;
ResultSet result = null;
try {
//2.获取连接(与数据库建立连接)
conn = DriverManager.getConnection(
pro.getProperty("url"),
pro.getProperty("username"),
pro.getProperty("password")
);
//3.准备数据,sql
String sql = "select * from dept"; //切记sql后不要添加;结尾
//4.封装处理块
state = conn.createStatement(); //静态处理块
//5.发送->接收结果集
result = state.executeQuery(sql);
//6.处理结果
while(result.next()){
int deptno = result.getInt(1);
String dname = result.getString(2);
String loc = result.getString(3);
System.out.println(deptno +"-->" +dname +"-->" +loc);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//7.关闭
if(result!=null){
try {
result.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(state!=null){
try {
state.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
增删改:
查询 excuteQuery ->结果集 增删改 excuteUpdate ->int 影响行数 事务默认自动提交,可以设置设置手动提交
public class JDBCDemo03 {
public static void main(String[] args){
//properties 从配置文件中读取数据
Properties pro = new Properties();
try {
pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
} catch (IOException e) {
e.printStackTrace();
}
//1.加载驱动(选择要连接的数据库) oracle.jdbc.driver.OracleDriver
try {
Class.forName(pro.getProperty("driver"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = null;
Statement state = null;
try {
//2.获取连接(与数据库建立连接)
conn = DriverManager.getConnection(
pro.getProperty("url"),
pro.getProperty("username"),
pro.getProperty("password")
);
//设置事务 手动提交
conn.setAutoCommit(false);
//3.准备数据,sql
String sql = "update dept set dname = '333333' where deptno = 70"; //切记sql后不要添加;结尾
//4.封装处理块
state = conn.createStatement(); //静态处理块
//5.发送->接收结果集
int rows = state.executeUpdate(sql);
//6.处理结果
if(rows>0){
System.out.println("添加成功");
conn.rollback();
}else{
System.out.println("添加失败");
conn.rollback();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//7.关闭
if(state!=null){
try {
state.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}