JAVA additions and deletions to check code __java

Source: Internet
Author: User
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;


Import java.sql.Statement;
public class DBTools {//defines a method that is used to get a "new" Connection object 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) {/To
Do 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.pri
Ntstacktrace ();
}} 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 {//new 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); The catch (Exception ex) {//TODO auto-generated catch block Ex.printstacktrace ();} finally {dbtools.closestate (state); D
BTOOLS.CLOSECONN (conn);
}///Get the maximum value of the current ID in a database 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); Remove the data from the ResultSet object if (Rs.next ()) {Maxid = Rs.getint ("Maxid");}


catch (Exception ex) {//TODO auto-generated catch block Ex.printstacktrace ();}
return ++MAXID;
//Get all Records 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);
Remove data from the ResultSet object 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);
Put the modified object into the Set class object that has already been created 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 to Al;
//Delete a user record 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); The catch (Exception ex) {//TODO auto-generated catch block Ex.printstacktrace ();} finally {dbtools.closestate (state); D
BTOOLS.CLOSECONN (conn); 
The record public ArrayList getuserinfobyinfo (String name,string email,string content) {Connection conn = null is obtained according to the given information;
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 + + email = ' +email+ ' ";} if (!" ".
Equals (content) && content!= null) {SQL + + and content like '% ' +content+ '% ';} sql+= ' ORDER by id DESC ';
rs = state.executequery (SQL);
Remove data from the ResultSet object 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);
Put the modified object into the Set class object that has already been created Arrauylist Al.add (Upo);
The catch (Exception ex) {//TODO auto-generated catch block Ex.printstacktrace ();} finally {dbtools.closers (RS);
Dbtools.closestate (state);
DBTOOLS.CLOSECONN (conn);
Return to Al;
 }


}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.