一、添加依赖
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
二、创建配置文件database.properties
oracleDriver=oracle.jdbc.driver.OracleDriver
oracleUrl=jdbc:oracle:thin:@192.168.180.144:1521:prod
oracleuser=lxm
oraclepwd=lxm
三、创建BaseDaoOracle基类
package com.atguigu.kb21.daooracle;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
public class BaseDaoOracle{
private static String driver;
private static String url;
private static String username;
private static String pwd;
static {
Properties properties = new Properties();
InputStream inputStream = BaseDaoOracle.class.getClassLoader().getResourceAsStream("database.properties");
try {
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
driver= properties.getProperty("oracleDriver");
url = properties.getProperty("oracleUrl");
username = properties.getProperty("oracleuser");
pwd = properties.getProperty("oraclepwd");
System.out.println(driver+"\t"+url+"\t"+username+"\t"+pwd);
}
// 封装Connection方法
public Connection getConnection(){
Connection connection = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
// 封装PreparedStatement方法
public Integer executeUpdate(String sql,Object...params){
Connection connection = this.getConnection();
PreparedStatement preparedStatement = null;
int num = -1;
try {
preparedStatement = connection.prepareStatement(sql);
if (null != params){
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i+1,params[i]);
}
}
num = preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
finally {
this.close(connection,preparedStatement);
}
return num;
}
// 封装close方法
public void close(Connection connection,PreparedStatement preparedStatement){
try {
if(null != connection){
connection.close();
}
if(null != preparedStatement){
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
四、创建Demo类
package com.atguigu.kb21.pojo;
public class Demo {
private Integer id;
private String name;
public Demo(Integer id, String name) {
this.id = id;
this.name = name;
}
public Demo() {
}
@Override
public String toString() {
return "Demo{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
五、创建DemoDao接口,定义功能
package com.atguigu.kb21.daooracle;
import com.atguigu.kb21.pojo.Demo;
public interface DemoDao {
// 新增数据
Integer saveDemo(Demo demo);
// 修改数据
Integer updateDemo(Demo demo);
// 删除数据
Integer delDemoById(Integer id);
}
六、创建DemoDaoImpl实现类,继承基类BaseDaoOracle,并且实现的功能
package com.atguigu.kb21.daooracle;
import com.atguigu.kb21.pojo.Demo;
public class DemoDaoImpl extends BaseDaoOracle implements DemoDao{
@Override
public Integer saveDemo(Demo demo) {
String sql = "insert into demo(id,name) values(?,?)";
Integer num = super.executeUpdate(sql, demo.getId(),demo.getName());
if(num >0)
System.out.println("新增数据成功!"+num);
return num;
}
@Override
public Integer updateDemo(Demo demo) {
String sql = "update demo set name = ? where id = ?";
Integer num = super.executeUpdate(sql,demo.getName(),demo.getId());
if(num >0)
System.out.println("修改数据成功!"+num);
return num;
}
@Override
public Integer delDemoById(Integer id) {
String sql = "delete from demo where id = ?";
Integer num = super.executeUpdate(sql,id);
if(num >0)
System.out.println("删除数据成功!"+num);
return num;
}
}
七、创建测试类DemoTest,测试功能是否执行成功
package com.atguigu.kb21.oracletests;
import com.atguigu.daooracle.DemoDaoImpl;
import com.atguigu.kb21.pojo.Demo;
import org.junit.Test;
public class DemoTest {
@Test
public void testDemoDaoInsert(){
DemoDaoImpl demoDao = new DemoDaoImpl();
Demo demo = new Demo(40,"张三");
demoDao.saveDemo(demo);
}
@Test
public void testDemoDaoUpdate(){
DemoDaoImpl demoDao = new DemoDaoImpl();
Demo demo = new Demo(40,"张麻子");
demoDao.updateDemo(demo);
}
@Test
public void testDemoDaoDelById(){
DemoDaoImpl demoDao = new DemoDaoImpl();
demoDao.delDemoById(40);
}
}