Java access to SQL Server database Tools

Source: Internet
Author: User

1  PackageCom.dao;2 3 ImportJava.sql.*;4 Importjava.util.ArrayList;5 Importjava.util.List;6 7 ImportCom.sun.jmx.snmp.Timestamp;8 9  Public Abstract classBasedao {Ten  OneString Driver = "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; AString url = "Jdbc:sqlserver://localhost:1433;database=newsmanagersystem"; -String user = "sa"; -String pwd = "sa"; the  -     PrivateConnection Conn; -     PrivatePreparedStatement stmt; -     PrivateResultSet rs; +      -      Public AbstractObject Createmodel (ResultSet rs)throwsSQLException; +  A     PrivateConnection getconnection ()throwsSQLException { at          -         Try { - Class.forName (driver); -}Catch(ClassNotFoundException e) { - e.printstacktrace (); -         } in          -         returndrivermanager.getconnection (URL, user, pwd); to     } +      -     Private voidCloseAll (Connection conn, Statement Stmt,resultset rs) { the          *         if(rs! =NULL){ $             Try{Panax Notoginseng rs.close (); -}Catch(SQLException e) { the e.printstacktrace (); +             } A         } the          +         if(stmt! =NULL) { -             Try { $ stmt.close (); $}Catch(SQLException e) { - e.printstacktrace (); -             } the         } - Wuyi         if(Conn! =NULL) { the             Try { - conn.close (); Wu}Catch(SQLException e) { - e.printstacktrace (); About             } $         } -     } -      -     Private voidPreExecute (String Sql,object...args)throwssqlexception{ Aconn =getconnection (); +stmt =conn.preparestatement (SQL); the          for(inti = 0; i < args.length; i++) { -Object p =Args[i]; $             if(pinstanceofjava.util.Date) { the                 if(!p.getclass (). GetName (). StartsWith ("java.sql.")){ theJava.util.Date Date =(java.util.Date) p; thep =NewTimestamp (Date.gettime ()); the                 } -             } inStmt.setobject (i+1, p); the         } the     } About      the     Private voidPostExecute () { the CloseAll (conn, stmt, RS); theconn =NULL; +stmt =NULL; -rs =NULL; the     }Bayi      the     protected intexecuteupdate (String sql,object...args) { the         intres =-1; -          -         Try { the PreExecute (Sql,args); theres =stmt.executeupdate (); the}Catch(SQLException e) { the e.printstacktrace (); -}finally{ the PostExecute (); the         } the         94         returnRes; the     } the      the     protectedList executeQuery (String sql,object...args) {98List List =NewArrayList (); About          -         Try {101 PreExecute (Sql,args);102rs =stmt.executequery ();103             104              while(Rs.next ()) { theObject obj =Createmodel (RS);106 list.add (obj);107             }108}Catch(SQLException e) {109 e.printstacktrace (); the}finally{111 PostExecute (); the         }113          the         returnlist; the     } the     117     protectedObject Executequeryfirst (String sql,object...args) {118Object obj =NULL;119          -         Try {121 preExecute (sql, args);122rs =stmt.executequery ();123             124             if(Rs.next ()) { theobj =Createmodel (RS);126             }127}Catch(SQLException e) { - e.printstacktrace ();129}finally{ the PostExecute ();131         } the         133         returnobj;134     }135     136     protectedObject executequeryscalar (String sql,object...args) {137Object obj =NULL;138         139         Try { $ preExecute (sql, args);141rs =stmt.executequery ();142             143             if(Rs.next ()) {144obj = Rs.getobject (1);145             }146}Catch(SQLException e) {147 e.printstacktrace ();148}finally{149 PostExecute (); Max         }151          the         returnobj;153     }154     155}

Java access to SQL Server database Tools

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.