日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java中po代码示例_java操作oracle常用的示例代码详解

發布時間:2024/7/23 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中po代码示例_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) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return conn;

}

public static void closeConn(Connection conn)

{

try {

if(conn != null)

{

conn.close();

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void closeState(Statement state)

{

try {

if(state != null)

{

state.close();

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void closeRs(ResultSet rs)

{

try {

if(rs != null)

{

rs.close();

}

} catch (SQLException e) {

// TODO Auto-generated catch block

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 {

// 新增user

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()+"')";

//System.out.println(sql);

state.executeUpdate(sql);

} catch (Exception ex) {

// TODO Auto-generated catch block

ex.printStackTrace();

}

finally

{

DBTools.closeState(state);

DBTools.closeConn(conn);

}

}

//得到一個數據庫中當前Id的最大值

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);

//從resultset對象中將數據取出

if(rs.next())

{

maxId = rs.getInt("maxId");

}

} catch (Exception ex) {

// TODO Auto-generated catch block

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();

String sql = "select * from notebook_user";

rs = state.executeQuery(sql);

//從resultset對象中將數據取出

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);

//將改對象放入已經創建好的集合類對象ArrauyList

al.add(upo);

}

} catch (Exception ex) {

// TODO Auto-generated catch block

ex.printStackTrace();

}

finally

{

DBTools.closeRs(rs);

DBTools.closeState(state);

DBTools.closeConn(conn);

}

return al;

}

// 刪除一條user記錄

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;

//System.out.println(sql);

state.executeUpdate(sql);

} catch (Exception ex) {

// TODO Auto-generated catch block

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();

String sql = "select * from notebook_user where 1=1 ";

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);

//從resultset對象中將數據取出

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);

//將改對象放入已經創建好的集合類對象ArrauyList

al.add(upo);

}

} catch (Exception ex) {

// TODO Auto-generated catch block

ex.printStackTrace();

}

finally

{

DBTools.closeRs(rs);

DBTools.closeState(state);

DBTools.closeConn(conn);

}

return al;

}

}

總結

以上是生活随笔為你收集整理的java中po代码示例_java操作oracle常用的示例代码详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。