Simple ways to operate MySQL: Statement, PreparedStatement

Source: Internet
Author: User

(1) mysql-connected tool class: Dbutil.java

 PackageCom.xuliugen.util;ImportJava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.PreparedStatement;ImportJava.sql.ResultSet;ImportJava.sql.SQLException;ImportJava.sql.Statement; Public  class dbutil {     Public StaticConnectionGetconn() {Connection conn =NULL;Try{Class.forName ("Com.mysql.jdbc.Driver"); conn = Drivermanager.getconnection ("jdbc:mysql://localhost:3306/Your database name","Your account","Your password"); }Catch(ClassNotFoundException e)        {E.printstacktrace (); }Catch(SQLException e)        {E.printstacktrace (); }returnConn } Public StaticPreparedStatementpreparestmt(Connection conn, String SQL) {PreparedStatement pstmt =NULL;Try{pstmt = conn.preparestatement (sql); }Catch(SQLException e)        {E.printstacktrace (); }returnpstmt; } Public StaticResultSetExecuteQuery(Statement stmt, String sql) {ResultSet rs =NULL;Try{rs = stmt.executequery (SQL); }Catch(SQLException e)        {E.printstacktrace (); }returnRs } Public Static void Close(Connection conn, Statement stmt, PreparedStatement prestatement, ResultSet rs) {if(Conn! =NULL) {Try{Conn.close (); }Catch(SQLException e)            {E.printstacktrace (); } conn =NULL; }if(stmt! =NULL) {Try{Stmt.close (); }Catch(SQLException e)            {E.printstacktrace (); } stmt =NULL; }if(Prestatement! =NULL) {Try{Prestatement.close (); }Catch(SQLException e)            {E.printstacktrace (); } prestatement =NULL; }if(rs! =NULL) {Try{Rs.close (); }Catch(SQLException e)            {E.printstacktrace (); } rs =NULL; }    }}

(2) database for inserting data using PreparedStatement:

 Public Boolean savecomment(Comment Comment)        {Connection Connection = Dbutil.getconn (); String sql ="INSERT into comment values (null,?,?,?,?)"; PreparedStatement PreparedStatement =NULL;BooleanFlag =false;Try{preparedstatement = connection.preparestatement (sql); Preparedstatement.setstring (1, Comment.getcommenttext () +""); Preparedstatement.setstring (2, Comment.getcommenttime () +""); Preparedstatement.setstring (3, Comment.getuserid () +""); Preparedstatement.setstring (4, Comment.getarticleid () +"");intIsOk = Preparedstatement.executeupdate ();if(IsOk >0) {return!flag; }Else{returnFlag }        }Catch(SQLException e)        {E.printstacktrace (); } dbutil.close (Connection,NULL, PreparedStatement,NULL);returnFlag }

(3) Use PreparedStatement to select data and read data:

 PublicList<comment>Getcommentdetail(intUseridintArticleID) {Connection Connection = Dbutil.getconn (); String sql ="SELECT * from comment C where c.userid=?" and c.articleid=? ";//write SQL statements, the first field does not need to be inserted, is automatically addedPreparedStatement PreparedStatement =NULL; List<comment> commentlist =NewArraylist<comment> ();Try{preparedstatement = connection.preparestatement (sql); Preparedstatement.setint (1, UserID); Preparedstatement.setint (2, ArticleID);//There is no need for incoming SQL statements in the productionResultSet rs = Preparedstatement.executequery ();//Determine if it is empty            if(Rs.next ()) { while(Rs.next ()) {//Iterate the information into the listComment Comment =NewComment (); Comment.setcommentid (Rs.getint ("Commentid")); Comment.setcommenttext (Rs.getstring ("Commenttext")); Comment.setcommenttime (Rs.getstring ("Commenttime")); Comment.setuserid (Rs.getint ("userid")); Comment.setarticleid (Rs.getint ("ArticleID"));                Commentlist.add (comment); }            }Else{return NULL; }        }Catch(SQLException e)        {E.printstacktrace (); } dbutil.close (Connection,NULL, PreparedStatement,NULL);returnCommentlist; }

(4) using statement to operate the database:

 PublicList<article>Getarticlemessage() {Connection Connection = Dbutil.getconn (); String sql ="SELECT * from article";//write SQL statements, the first field does not need to be inserted, is automatically addedStatement Statement =NULL; List<article> articlelist =NewArraylist<article> ();Try{statement = Connection.createstatement (); ResultSet rs = statement.executequery (SQL);//Determine if it is empty            if(Rs.next ()) { while(Rs.next ()) {//Iterate the information into the listArticle Article =NewArticle (); Article.settitle (Rs.getstring ("title")); Article.setcontext (Rs.getstring ("Context")); Article.setarticletime (Rs.getstring ("Articletime")); Article.setuserid (Rs.getint ("userid")); Article.setarticleid (Rs.getint ("ArticleID"));                Articlelist.add (article); }            }Else{return NULL; }        }Catch(SQLException e)        {E.printstacktrace (); } dbutil.close (connection, statement,NULL,NULL);returnArticlelist; }

Simple ways to operate MySQL: Statement, PreparedStatement

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.