JAVA使用JDBC连接ORACLE数据库改进优化版
JAVA使用JDBC连接ORACLE数据库基础版open in new window
JAVA使用JDBC连接ORACLE数据库改进优化版open in new window
构建工厂优化JAVA使用JDBC连接ORACLE数据库open in new window
maven的oracle依赖
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
package xin.dbtool;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBtool {
static String driver = "oracle.jdbc.driver.OracleDriver";
static String url = "jdbc:oracle:thin:localhost:1521:ORCL";
static String name = "MyTest";
static String pass = "mytest";
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, name, pass);
return conn;
}
public static void close(Connection conn, PreparedStatement ps, ResultSet rs) throws SQLException {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
}
public static void close(Connection conn, PreparedStatement ps) throws SQLException {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
}
}
DeptTable属性类
package xin.model;
public class DeptTable {
private int deptNo;
private String dName;
private String city;
public DeptTable(){}
public DeptTable(int deptNo,String dName,String city){
this.deptNo = deptNo;
this.dName = dName;
this.city = city;
}
public DeptTable(String dName,String city){
this.dName = dName;
this.city = city;
}
public int getDeptNo() {
return deptNo;
}
public void setDeptNo(int deptNo) {
this.deptNo = deptNo;
}
public String getdName() {
return dName;
}
public void setdName(String dName) {
this.dName = dName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "部门号是:"+deptNo+" 部门名是:"+dName+" 部门所在地是:"+city;
}
}
DeptTableServices业务层
package xin.services;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import xin.dbtool.DBtool;
import xin.model.DeptTable;
public class DeptTableServices {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:localhost:1521:ORCL";
String name = "MyTest";
String pass = "mytest";
List<DeptTable> list = new ArrayList<DeptTable>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
public void showAllDept(){
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,name,pass);
String sql ="select * from dept";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
String pass = rs.getString(3);
System.out.println("部门号是:"+id+" 部门名是:"+name+" 部门所在地是:"+pass);
}
rs.close();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<DeptTable> showAllDept1(){
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,name,pass);
String sql ="select * from dept";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
DeptTable dept = new DeptTable(rs.getInt(1),rs.getString(2),rs.getString(3));
list.add(dept);
}
rs.close();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public boolean addDept(String...val){
try {
conn = DBtool.getConnection();
String sql = "insert into dept values(DEPT_ID_SEQ.NEXTVAL,?,?)";
ps = conn.prepareStatement(sql);
for(int i = 0;i<val.length;i++){
ps.setString(i+1,val[i]);
}
int rs = ps.executeUpdate();
while(rs>0){
return true;
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
finally{
try {
DBtool.close(conn, ps);
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
public boolean addDept(DeptTable dept){
try {
conn = DBtool.getConnection();
String sql = "insert into dept values(DEPT_ID_SEQ.NEXTVAL,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1,dept.getdName());
ps.setString(2,dept.getCity());
int rs = ps.executeUpdate();
while(rs>0){
return true;
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
finally{
try {
DBtool.close(conn, ps);
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
public boolean deleteDept(int id){
try {
conn = DBtool.getConnection();
String sql = "delete from dept where deptno = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1,id);
int rs = ps.executeUpdate();
if(rs>0){
return true;
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
finally{
try {
DBtool.close(conn, ps);
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
public boolean updateDept(DeptTable dept){
try {
conn = DBtool.getConnection();
String sql = "update dept set deptno = DEPT_ID_SEQ.NEXTVAL,dname = ?,city = ? "
+ "where deptno = ?";
ps = conn.prepareStatement(sql);
ps.setString(1,dept.getdName());
ps.setString(2,dept.getCity());
ps.setInt(3, dept.getDeptNo());
int rs = ps.executeUpdate();
if(rs>0){
return true;
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
finally{
try {
DBtool.close(conn, ps);
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
}
DeptTest测试
package xin.test;
import java.util.List;
import xin.model.DeptTable;
import xin.services.DeptTableServices;
public class DeptTest {
public static void main(String[] args) {
DeptTableServices dept = new DeptTableServices();
String[] val={"hahaha","xixixi"};
boolean f = dept.addDept(val);
if (f){
System.out.println(" ADD OK");
}
else{
System.out.println(" ADD FALSE");
}
}
}