JAVA增刪改查代碼__JAVA

來源:互聯網
上載者:User
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 blocke.printStackTrace();}return conn;}public static void closeConn(Connection conn){try {if(conn != null){conn.close();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void closeState(Statement state){try {if(state != null){state.close();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void closeRs(ResultSet rs){try {if(rs != null){rs.close();}} catch (SQLException e) {// TODO Auto-generated catch blocke.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 {// 新增userpublic 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 blockex.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 blockex.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);//將改對象放入已經建立好的集合類對象ArrauyListal.add(upo);}} catch (Exception ex) {// TODO Auto-generated catch blockex.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 blockex.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);//將改對象放入已經建立好的集合類對象ArrauyListal.add(upo);}} catch (Exception ex) {// TODO Auto-generated catch blockex.printStackTrace();}finally{DBTools.closeRs(rs);DBTools.closeState(state);DBTools.closeConn(conn);}return al;}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.