In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to carry out JDBC case analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Example 1 of JDBC, first configure the following in the configuration file (system.properties):
Driver=com.microsoft.sqlserver.jdbc.SQLServerDriver url=jdbc:sqlserver://localhost:1433;databaseName=mp username=sa password=mengya
JDBC example 2, wrote a utility class for SQLDB
PublicclassSQLDBConnection {privateInputStreaminputstr; privatePropertiespro; privatestaticSQLDBConnectionsqldb=null;// private constructor privateSQLDBConnection () {inputstr=this.getClass (). GetResourceAsStream ("/ system.properties"); pro=newProperties (); try {pro.load (inputstr);} catch (IOExceptione) {e.printStackTrace ();} try {Class.forName (pro.getProperty ("driver")); / / register the driver, only once} catch (ClassNotFoundExceptione) {e.printStackTrace () }} / / Singleton pattern publicstaticSQLDBConnectiongetSQLDBConnection () {if (sqldb==null) {synchronized (SQLDBConnection.class) {if (sqldb==null) {sqldb=newSQLDBConnection ();} returnsqldb;} / / get the connection to the database publicConnectionGetConnection () {Connectionconn=null;try {conn=DriverManager.getConnection ("url"), pro.getProperty ("username"), pro.getProperty ("password");} catch (SQLExceptione) {e.printStackTrace ();} returnconn;}
Example 3 of JDBC, write the interface of Studao
/ / release resource publicstaticvoidfree (ResultSetrs,Statementsta,Connectionconn) {try {if (rswatches null) {rs.close ();}} catch (Exceptione) {e.printStackTrace ();} finally {try {if (staple null) {sta.close ();}} catch (Exceptione) {e.printStackTrace ();} finally {if (connexual null) {try {conn.close ();} catch (SQLExceptione) {e.printStackTrace () } publicinterfaceStudaointer {publicvoidaddStu (Stustu); publicvoiddelStu (intsid); publicvoidupdStu (Stustu); publicStugetOneStu (intsid); publicListgetAllStu ();}
JDBC example 4, write your own defined RuntimeException
PublicclassMySQLExceptionextendsRuntimeException {privatestaticfinallongserialVersionUID=1L;}
JDBC example 5, write the implementation class of Studao
PublicclassStuDAOImpleimplementsStudaointer {privateConnectionconn; privatePreparedStatementpre; privateResultSetrs; publicvoidaddStu (Stustu) {Stringsql= "insertintostuvalues ()"; conn=SQLDBConnection.getSQLDBConnection (). GetConnection (); try {pre=conn.prepareStatement (sql); pre.setString (1memstu.getSname ()); pre.setString (2memstu.getSsex ()); pre.setDate (3Min newjava.sql.Date (stu.getSbrith (). GetTime ()); pre.executeUpdate ();} catch (SQLExceptione) {e.printStackTrace (); thrownewMySQLException () / / throw finally {SQLDBConnection.free (rs,pre,conn);}} publicvoiddelStu (intsid) {Stringsql= "deletestuwheres_id=?"; conn=SQLDBConnection.getSQLDBConnection (). GetConnection (); try {pre=conn.prepareStatement (sql); pre.setInt (1 philosophy sid); pre.executeUpdate ();} catch (SQLExceptione) {e.printStackTrace (); thrownewMySQLException ();} finally {SQLDBConnection.free (rs,pre,conn). Is it helpful for you to read the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.