My Study Note of JDBC (1)

Source: Internet
Author: User

# JDBC-The Java™tutorials
# Study Note of JDBC
# Victor
# 2016.05.31

JDBC Study Note----Connect to Database

In general, the following 5 steps are required to execute an SQL statement using JDBC:
1> Establishing a Connection | Establish a connection
2> Constructs a statement | Create a statement
3> Execution Statement | Execute the query
4> Processing Results | Process the ResultSet object
5> Closing Connections | Close the connection

1. Establish a connection
1.1 Registe Driver
Class.forName ("Oracle.jdbc.driver.OracleDriver");
# If you are using a version of JDBC 4.0, this step can be ignored
1.2 Get Connection
con = drivermanger.getconnection (Dburl,username,password);
# Dburl refers to the database that JDBC driver uses to connect
# Dburl contains the name and configuration of the database
# The specific URL syntax is determined by the database used

1.2.1 URL format for Oracle database
Jdbc:oracle:<drivertype>:@<database>
# Specific content:
# https://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Code:

 PackageMyjdbc;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importjava.util.InvalidPropertiesFormatException;Importjava.util.Properties; Public classMyjdbcutil { PublicString DBMS;  PublicString DbName;  PublicString UserName;  PublicString password; PrivateString ServerName; Private intPortNumber; PrivateProperties prop;  PublicMyjdbcutil (String filename)throwsFileNotFoundException, invalidpropertiesformatexception, ioexception{ This. setproperties (filename); }            /*get Properties from the. properties file * and set these values to corresponding fields **/      Private voidSetProperties (String fileName)throwsfilenotfoundexception,ioexception,invalidpropertiesformatexception { This. Prop =NewProperties (); InputStream FIS= This. GetClass (). getResourceAsStream (FileName);                        Prop.load (FIS);  This. DBMS = This. Prop.getproperty ("DBMS");  This. DbName = This. Prop.getproperty ("database_name");  This. UserName = This. Prop.getproperty ("user_name");  This. Password = This. Prop.getproperty ("Password");  This. ServerName = This. Prop.getproperty ("SERVER_NAME");  This. PortNumber = Integer.parseint ( This. Prop.getproperty ("Port_number")); System.out.println ("Set The following properties:"); System.out.println ("DBMS:" +DBMS); System.out.println ("DbName:" +dbName); System.out.println ("UserName:" +userName); System.out.println ("Password:" +password); System.out.println ("ServerName:" +serverName); System.out.println ("PortNumber:" +portnumber); }            /*Step 1 to execute query using JDBC * Get connection with connection properties **/       PublicConnection getconnection ()throwsclassnotfoundexception, sqlexception{class.forname ("Oracle.jdbc.driver.OracleDriver"); System.out.println ("Oracle JDBC Driver registered!"); Connection Conn=NULL; Properties Connectionprops=NewProperties (); Connectionprops.put ("User", This. UserName); Connectionprops.put ("Password", This. Password); if( This. Dbms.equals ("Oracle") ) {String Dburl= "JDBC:" + This. dbms+ ":" +dbname+ ": @" + This. servername+ ":" + This. portnumber+ "/xe"; System.out.println ("Dburl is:" +Dburl); Conn=drivermanager.getconnection (Dburl, connectionprops); System.out.println ("Connected to Database successfully!"); }                    returnConn; }}
JDBC Connection

Test Code:

 package   Myjdbc;  public  class   Testjdbc { static  void   main (string[] args) {//  TODO auto-generated method stub  try  {myjdbcutil db  = new  MYJD            Bcutil ("mydb.properties" );        Db.getconnection ();  catch   (Exception e) {E.printstacktrac        E (); }    }}
Test

OutPut:

1522 Oracle JDBC Driver registered! dburl Is:jdbc:oracle:thin: @localhost: 1522/xeconnected to database successfully!
View Code

Pros

Dbms=oracle
Database_name=thin
User_name=test
Password=test
Server_name=localhost
port_number=1522
JDBC::: @localhost: 1522/xe

My Study Note of JDBC (1)

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.