Class.forName () and newinstance ()

Source: Internet
Author: User

1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 ImportJava.sql.ResultSet;4 Importjava.sql.Statement;5 Importjava.util.Date;6 7 8  Public classNewinstanceexample {9 Ten      Public Static voidMain (string[] args)throwsException { One         //Class.forName () requires the JVM to find and load the specified class A         //newinstance () Gets an instance of the class -         //newinstance () method differs from the New keyword: one is a method, one is a keyword, and newinstance () can only call a parameterless constructor -         //forname and newinstance gain greater flexibility and provide a means of decoupling theDate date = Java.util.Date.class. newinstance (); -         LongTime =date.gettime (); - System.out.println (time); -          +class<?> Dateclass = Class.forName ("Java.util.Date"); -Date Date2 =(Date) dateclass.newinstance (); +         LongTime2 =date2.gettime (); A System.out.println (time2); at          - mysqlconnection (); -          -     } -      -      Public Static voidMysqlconnection ()throwsException { in         //It is explicitly required in the JDBC specification that the driver class must register itself with DriverManager -         //similar to the following: to         /* + Public class Myjdbcdriver implements Driver { - Static { the Drivermanager.registerdriver (New Myjdbcdriver ()); *             } $         }Panax Notoginseng         */ -         //Class.forName () executes a static block of code when the Com.mysql.jdbc.Driver is loaded, registering itself with DriverManager theClass.forName ("Com.mysql.jdbc.Driver"); +String url = "Jdbc:mysql://localhost/test?useunicode=true&&characterencoding=utf-8&autoreconnect=true "; AString user = "root"; theString password = "123456"; +          -Connection conn =NULL;  $Statement stmt =NULL;  $ResultSet rs =NULL;  -         Try {   -conn =drivermanager.getconnection (URL, user, password); thestmt =conn.createstatement (); -String sql = "Select Deptno,deptname from dept";//Dept This table has a deptno,deptname fieldWuyirs = stmt.executequery (SQL);//Execute SQL statement the              while(Rs.next ()) { -System.out.print (Rs.getint ("deptno") + ""); WuSystem.out.println (rs.getstring ("Deptname")); -             } About}Catch(Exception e) { $             Throwe; -}finally { - rs.close (); - stmt.close (); A conn.close (); +         }   the     } -}

Class.forName () and newinstance ()

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.