JDBC Connect SQL Server database (easy code)

Source: Internet
Author: User

1 Package Pmsdao;2 3 import java.sql.Connection;4 import Java.sql.DriverManager;5 import java.sql.PreparedStatement;6 import Java.sql.ResultSet;7 import java.sql.SQLException;8 9  Public classBasedao {Ten     //Driver class full name (package name. Class name) One     Private StaticFinal String DRIVER ="Com.microsoft.sqlserver.jdbc.SQLServerDriver"; A     //URL of the connection -     Private StaticFinal String URL ="jdbc:sqlserver://localhost\\sqlexpress:1433;databasename=terminalequipment"; -     //log in to SQL Server user name and password the     Private StaticFinal String USERNAME ="SA"; -     Private StaticFinal String PWD ="ghp418512"; -  -     //Database Object Declaration +     Private StaticPreparedStatement PST =NULL; -     Private StaticResultSet rs =NULL; +     Private StaticConnection con =NULL; A  at     /** - * Load Driver -      */ -     Static { -         Try { - Class.forName (DRIVER); in}Catch(ClassNotFoundException e) { -             //TODO auto-generated Catch block to e.printstacktrace (); +         } -     } the  *     /** $ * Establish a connectionPanax Notoginseng      */ -      Public StaticConnection Getcon () { the         Try { +Con =drivermanager.getconnection (URL, USERNAME, PWD); A             returncon; the}Catch(SQLException e) { +             //TODO auto-generated Catch block - e.printstacktrace (); $         } $         return NULL; -     } -  the     /** - * Query ExecutionWuyi      *  the * @param SQL - * Parameterized SQL statements executed Wu * @param params - * Object array, encapsulating all SQL statement parameters About * @return ResultSet Returns the result set after execution $      */ -      Public StaticResultSet ExecQuery (String sql, object[]params) { -         Try { - Getcon (); APST =con.preparestatement (SQL); +  theSetpreparestatementparams (params); -rs =pst.executequery (); $}Catch(SQLException e) { the             //TODO auto-generated Catch block the e.printstacktrace (); the         } the         returnrs; -  in     } the  the     /** About * Perform additions and deletions to SQL operation method the      *  the * @param SQL the * Parameterized SQL statements executed + * @param params - * Object array, encapsulating all SQL statement parameters the * @return The number of rows affected, 1 indicates an exception occurredBayi      */ the      Public intExecupdate (String sql, object[)params) { the  - Getcon (); -         Try { thePST =con.preparestatement (SQL); the  theSetpreparestatementparams (params); the  -             intAffectrows =pst.executeupdate (); the             returnaffectrows; the}Catch(SQLException e) { the             //TODO auto-generated Catch block94 e.printstacktrace (); the}finally { the Free (RS, PST, con); the         }98         return-1; About  -     }101 102     /**103 * Set parameters for Preparestatement104      *  the * @param params106 * Parameter Array107 * @throws SQLException108      */109     Private Static voidSetpreparestatementparams (object[]params) the throws SQLException {111         if(params!=NULL) { the              for(inti =0; I <params. length; i++) {113Pst.setobject (i +1,params[i]); the             } the         } the     }117 118     /**119 * Close Connection,preparestatement,result -      * 121 * @param rs122 * @param PST123 * @param con124      */ the      Public Static voidFree (ResultSet RS, PreparedStatement PST, Connection con) {126         Try {127             if(rs! =NULL) { - rs.close ();129             } the             if(PST! =NULL) {131 pst.close (); the             }133             if(Con! =NULL) {134 con.close ();135             }136}Catch(SQLException e) {137 e.printstacktrace ();138         }139  $     }141}

JDBC Connect SQL Server database (easy code)

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.