Java-concise code

Source: Internet
Author: User

Classes used to add, delete, modify, and query databases. No matter what SQL statements are, a stream of words is better. Thanks!

 

 

Package COM. project. database; <br/> Import Java. io. unsupportedencodingexception; <br/> Import Java. SQL. connection; <br/> Import Java. SQL. drivermanager; <br/> Import Java. SQL. preparedstatement; <br/> Import Java. SQL. resultset; <br/> Import Java. SQL. sqlexception; <br/> Import COM. project. utils. encodingmanager; <br/> public class operatordatabase {<br/> private connection = NULL; <br/> private pre Paredstatement PST = NULL; <br/> encodingmanager em = new encodingmanager (); <br/> private connection createconnection () throws classnotfoundexception, sqlexception <br/>{< br/> class. forname ("com. mySQL. JDBC. driver "); <br/> connection = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306/Project", "root", "lichaoever"); <br/> return this. connection; <br/>}</P> <p>/** <br/> * query <br/> * @ P Aram SQL <br/> * @ Param objects <br/> * @ return <br/> * @ throws sqlexception <br/> * @ throws classnotfoundexception <br/> */< br/> Public resultset executequery (string SQL, object... objects) throws sqlexception, classnotfoundexception <br/>{< br/> If (connection = NULL) This. createconnection (); <br/> PST = connection. preparestatement (SQL); <br/> This. bindparameter (PST, objects); <br/> return PST. Executequery (); <br/>}</P> <p>/** <br/> * execute the update, modification Operation <br/> * @ Param SQL <br/> * @ Param objects <br/> * @ return <br/> * @ throws classnotfoundexception <br/> * @ throws sqlexception <br/> */<br/> Public int executeupdate (string SQL, object... objects) throws classnotfoundexception, sqlexception <br/>{< br/> If (connection = NULL) This. createconnection (); <br/> PST = connection. preparestatement (SQL ); <Br/> This. bindparameter (PST, objects); <br/> return pst.exe cuteupdate (); <br/>}</P> <p>/** <br/> * bind parameters <br/> * @ Param PST <br/> * @ Param objects parameter list <br/> * @ throws sqlexception <br/> */<br/> Public void bindparameter (preparedstatement PST, object... objects) throws sqlexception <br/>{< br/> If (Objects = NULL) return; <br/> int I = 1; <br/> for (Object object: objects) <br/>{< br/> PST. set Object (I ++, object); <br/>}</P> <p> Public void close () throws sqlexception <br/>{< br/> This. close (null); <br/>}</P> <p> Public void close (resultset RS) throws sqlexception <br/>{< br/> If (RS! = NULL) <br/>{< br/> Rs. Close (); <br/> rs = NULL; <br/>}< br/> If (Pst! = NULL) <br/>{< br/> PST. Close (); <br/> PST = NULL; <br/>}< br/> If (connection! = NULL) <br/>{< br/> connection. close (); <br/> connection = NULL; <br/>}</P> <p >}< br/>}

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.