java 操作oracle 源码_java对oracle的常用操作示例源码

将做工程过程重要的代码片段做个收藏,下面资料是关于java对oracle的常用操作示例的代码。

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class DBTools {

public static Connection getConnection()

{

Connection conn = null;

String driverName = "oracle.jdbc.driver.OracleDriver";

String url = "jdbc:oracle:thin:@localhost:1521:ora9i";

String userName = "scott";

String passWord = "tiger";

try {

Class.forName(driverName);

conn = DriverManager.getConnection(url,userName,passWord );

} catch (Exception e) {

e.printStackTrace();

}

return conn;

}

public static void closeConn(Connection conn)

{

try {

if(conn != null)

{

conn.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

public static void closeState(Statement state)

{

try {

if(state != null)

{

state.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

public static void closeRs(ResultSet rs)

{

try {

if(rs != null)

{

rs.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

import java.sql.ResultSet;

import java.sql.Statement;

import java.util.ArrayList;

import com.tianyitime.notebook.support.userPO.UserPO;

import com.tianyitime.notebook.support.util.DBTools;

public class UserDAO {

public void saveUserInfo(UserPO upo)

{

Connection conn = null;

Statement state = null;

try {

conn = DBTools.getConnection();

state = conn.createStatement();

String sql = "insert into notebook_user values ("+getMaxId()+",'"+upo.getYhm()+"','"+upo.getEmail()+"','"+upo.getContent()+"')";

state.executeUpdate(sql);

} catch (Exception ex) {

ex.printStackTrace();

}

finally

{

DBTools.closeState(state);

DBTools.closeConn(conn);

}

}

private int getMaxId()

{

Connection conn = null;

Statement state = null;

ResultSet rs = null;

int maxId = 0;

try {

conn = DBTools.getConnection();

state = conn.createStatement();

String sql = "select max(id) maxId from notebook_user";

rs = state.executeQuery(sql);

if(rs.next())

{

maxId = rs.getInt("maxId");

}

} catch (Exception ex) {

ex.printStackTrace();

}

return ++maxId;

}

public ArrayList getUserInfo()

{

Connection conn = null;

Statement state = null;

ResultSet rs = null;

UserPO upo = null;

ArrayList al = new ArrayList();

try {

conn = DBTools.getConnection();

state = conn.createStatement();

rs = state.executeQuery(sql);

while(rs.next())

{

upo = new UserPO();

int id = rs.getInt("id");

String yhm = rs.getString("yhm");

String email = rs.getString("email");

String content = rs.getString("content");

upo.setId(id);

upo.setYhm(yhm);

upo.setEmail(email);

upo.setContent(content);

al.add(upo);

}

} catch (Exception ex) {

ex.printStackTrace();

}

finally

{

DBTools.closeRs(rs);

DBTools.closeState(state);

DBTools.closeConn(conn);

}

return al;

}

public void deleteUserInfo(int id)

{

Connection conn = null;

Statement state = null;

try {

conn = DBTools.getConnection();

state = conn.createStatement();

String sql = "delete from notebook_user where id="+id;

state.executeUpdate(sql);

} catch (Exception ex) {

ex.printStackTrace();

}

finally

{

DBTools.closeState(state);

DBTools.closeConn(conn);

}

}

public ArrayList getUserInfoByInfo(String name,String email,String content)

{

Connection conn = null;

Statement state = null;

ResultSet rs = null;

UserPO upo = null;

ArrayList al = new ArrayList();

try {

conn = DBTools.getConnection();

state = conn.createStatement();

if(!"".equals(name) && name != null)

{

sql += " and yhm like '%"+name+"%'";

}

if(!"".equals(email) && email != null)

{

sql += " and email = '"+email+"'";

}

if(!"".equals(content) && content != null)

{

sql += " and content like '%"+content+"%'";

}

sql+=" order by id desc";

rs = state.executeQuery(sql);

while(rs.next())

{

upo = new UserPO();

int id = rs.getInt("id");

String yhm = rs.getString("yhm");

String femail = rs.getString("email");

String fcontent = rs.getString("content");

upo.setId(id);

upo.setYhm(yhm);

upo.setEmail(femail);

upo.setContent(fcontent);

al.add(upo);

}

} catch (Exception ex) {

ex.printStackTrace();

}

finally

{

DBTools.closeRs(rs);

DBTools.closeState(state);

DBTools.closeConn(conn);

}

return al;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值