"JDBC" connects the database from shallow into deep

Source: Internet
Author: User

A. Recall

Remember the previous self-study, just contact the database, the most basic theoretical knowledge and methods of connecting database, now review the basics and summarize as follows!

1.JDBC basic process for connecting to a database

Download the JDBC driver , Url,root,password, connection database for JDBC Connection, Get statement related classes close JDBC related objects by completing the add-and- revise

2. Detailed ①JDBC driver loading

There are 4 ways to talk about JDBC loading drivers, but it's not quite clear. This uses the Java reflection to complete the driver loading. But only if the appropriate jar package is required to load successfully

MAVEN Configuration Jar Package:

        <!--mysql driver-        <dependency>            <groupId>mysql</groupId>            <artifactId> mysql-connector-java</artifactid>            <version>5.1.9</version>            <scope>runtime</ Scope>        </dependency>

MySQL Driver: com.mysql.jdbc.Driver

Oracle Driver: Oracle.jdbc.driver.OracleDriver

For example:

Class.forName (com.mysql.jdbc.Driver);//Initialize the JDBC driver and load the appropriate driver into the JVM
② Connection Database

The DriverManager class provided by the Java SE API is used here to get the connection interface of the connection

MySQL url:jdbc:mysql://localhost:3306/test (database name)

Oracle's Url:jdbc:oracle:thin: @localhost: 1521:test (database name)

For example:

String url = "Jdbc:mysql://localhost:3306/test"= "root" = "root" = Drivermanager.getconnection (URL,USER,PASSWD);
③ acquisition of statement related classes for additional pruning

Interface Statement(can only be used to perform static code + stitching):

Method: Boolean Execute (String sql);

int executeupdate (String sql);//executable or delete

ResultSet executeQuery (String sql);//execute query function

Statement Statement = connection.createstatement ();
= Statement.executequery ("SELECT * FROM table1");//The query uses the ExecuteQuery method to return resultset while( Resultset.next ()) { System.out.print (Resultset.getobject (1) + "---" +resultset.getobject (2));
System.out.print (resultset.getstring (1) + + "---" +resultset.getdate (2));}

Interface preparedstatement(dynamic code can be executed):

Method: Boolean execute ();

int executeupdate ();//executable or delete

ResultSet executeQuery ();//execute query function

String sql = "Update test set name =?" WHERE id =? "  = connection.preparestatement (sql); ps.setstring (1, "SSX"); Ps.setint (2,1); Ps.execute ();

In addition, interface resultsetmetadate(using the Getmetadate () method) can be obtained by resultset

Method: String getcatalogname (int column) gets the table name

int getColumnCount (): Gets the number of columns of the result set

String getcolumnlabel (int column): Gets the column name of the corresponding column

3. Encapsulating JDBC

You can see here: it's very well written.

"JDBC" connects the database from shallow into deep

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.