JDBC Learning Note 1

Source: Internet
Author: User

JDBC (Java Database Connectivity)
First, based on the socket+ database underlying protocol
Java Sun------Standard (interface) java.sql.*;
JDBC Sun provides a set of interfaces for easy connection to the database. This set of interfaces by the major database vendors to achieve, the final database vendors will give a set of jar package.

Second, the interface

Driver data-driven, directly converts the code we write into a database network protocol
Statement executing a static SQL statement
PreparedStatement Pre-compilation
Connection getting a connection to a database
ResultSet accept the result set returned by the database

Dirvermanager Drive management class for driver management
Provided by Ojdbc14.jar developers
Ojdbc14_g.jar will enter a large number of test statements when running, the data developer
Ojdbc5.jar because the JDK version is too high, use this JAR package

Four elements connected to a database

Driver class
Private String driver= "Oracle.jdbc.OracleDriver";
Request Resource Path
Private String url= "Jdba:oracle:thin: @localhost: 1521:xe";
User name
Private String user= "Victor";
Password
Private String password= "Victor";

1, driver--refers to the package name + class name
Oracle.jdbc.driver.OracleDriver
2, Url:jdbc:oracle:thin: @localhost: 1521:xe
Thin represents the connection method
@localhost the IP address of the server where the database is requested
1521 the port number of the database
The port number of the Web page 8080
Name of XE Database
3, User user name---"Own future users to connect the operation
4. Password login password

Properties collection
Storage is key--value, generally only for string use
Stored value setproperty (key,value), type is string
Value GetProperty (key); key must be a string
You can load normal file files, or files that end in. properties
The contents of the file must be = connected, do not appear space

Operation Flow:

1. Registered Driver
2. Get the connection to the database
3. Create a statement object
4. Execute SQL statements
5. Close Resources

Execute (SQL) Boolean executes the SQL statement when the execution succeeds true, and vice versa false
executeupdate (SQL) int represents the row count affected
ExecuteQuery (SQL) ResultSet Returns a result set, typically used in query statements to obtain data from a database

Three ways to load drivers: Automatically load reflection creation, manually load instantiation, virtual machine pass-through parameter creation

Import Java.sql.DriverManager;
Import java.sql.SQLException;
public class Touch {
Four elements to connect to a database
Driver class
Private String driver= "Oracle.jdbc.OracleDriver";
Request Resource Path
Private String url= "Jdba:oracle:thin: @localhost: 1521:xe";
User name
Private String user= "Victor";
Password
Private String password= "Victor";
public void Connect () {
1. Automatic registration Drive [via Reflection registration Drive]
Java.sql.Connection con;
try {
Load Driver
Class.forName (driver);
Get database connection
Con=drivermanager.getconnection (URL, user, password);
System.out.println (con);
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public void Connect1 () {
2, registration driver, instantiation, manual registration
Java.sql.Connection con;
try {
Load Driver
Driver dri=new oracledriver ();
Get database connection
Con=drivermanager.getconnection (URL, user, password);
System.out.println (con);
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public void Connect2 () {
3, virtual machine transmission parameters
-djdbc.drivers=oracle.jdbc.oracledriver
Java.sql.Connection con;
try {
Get database connection
Con=drivermanager.getconnection (URL, user, password);
System.out.println (con);
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public static void Main (string[] args) {
Touch t=new Touch ();
T.connect ();
T.connect1 ();
T.connect2 ();
}
}

Two ways to insert data "with parameters and without parameters" and manually Commit transactions

Import Java.sql.Driver;
Import Java.sql.DriverManager;
Import java.sql.SQLException;

Import Oracle.jdbc.driver.OracleDriver;

public class Linkinsert {
Private String url= "Jdbc:oracle:thin: @localhost: 1521:xe";
Private String user= "Victor";
Private String password= "Victor";
Insert Data First Way
public void link () {
Java.sql.Connection con;
Java.sql.Statement STA;
Java.sql.ResultSet Res;
try {
Load Driver
Class.forName (driver);
Get database connection
Con=drivermanager.getconnection (URL, user, password);
Create a statement object
Sta=con.createstatement ();
String sql= "INSERT into Lanzhou values (2, ' Lisi ', ' femal ')";
Execute SQL statement
Res=sta.executequery (SQL);
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Insert Data First Way
public void Link1 (int id, String name,string gender) {
Java.sql.Connection con;
Java.sql.Statement STA;
Java.sql.ResultSet Res;
Driver driver=new oracledriver ();
try {
Drivermanager.registerdriver (driver);
Con=drivermanager.getconnection (URL, user, password);
Sta=con.createstatement ();
String sql= "INSERT into Lanzhou values (" +id+ "," +name+ "', '" +gender+ "')";
Res=sta.executequery (SQL);
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}
Submit things Manually
public void Link2 () {
Java.sql.Connection con = null;
Java.sql.Statement sta = null;
Java.sql.ResultSet res = null;
First Step: Register driver
Driver driver=new oracledriver ();
try {
Drivermanager.registerdriver (driver);
Part II: Getting connections
Con=drivermanager.getconnection (URL, user, password);
Manually submit a thing's settings, default to True
Con.setautocommit (FALSE);
Step three: Create a statement object
Sta=con.createstatement ();
String sql= "INSERT into Lanzhou values (4, ' Wangwu ', ' femal ')";
Fourth step: Execute the SQL statement
Res=sta.executequery (SQL);
Fifth Step: Submit Things
Con.commit ();
} catch (SQLException e) {
Things Roll back, undo
try {
Con.rollback ();
} catch (SQLException E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
if (res!=null)
Res.close ();
if (sta!=null)
Sta.close ();
if (con!=null)
Con.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
public static void Main (string[] args) {
Linkinsert l=new Linkinsert ();
L.link ();
L.LINK1 (3, "Lisi", "male");
L.link2 ();
}
}

Two queries "with parameters and without parameters"

Import Java.sql.Driver;
Import Java.sql.DriverManager;
Import java.sql.SQLException;

Import Oracle.jdbc.driver.OracleDriver;

public class Connection {
Private String url= "Jdbc:oracle:thin: @localhost: 1521:xe";
Private String user= "Victor";
Private String password= "Victor";
public void Connect () {
Java.sql.Connection Con=null;
Java.sql.Statement Sta=null;
Java.sql.ResultSet Res=null;
Driver driver=new oracledriver ();
try {
Drivermanager.registerdriver (driver);
Con=drivermanager.getconnection (URL, user, password);
Sta=con.createstatement ();
String sql= "SELECT * from Lanzhou";
Execute the SQL statement, with the result put back, so use the following statement
Res=sta.executequery (SQL);
Set.next returns a Boolean if the next position has a value of true
while (Res.next ()) {
System.out.println ("id=" +res.getint (1) + "; "+" Name= "+res.getstring (2) +"; "+" gender= "+res.getstring (3));
System.out.println ("Name=" +res.getstring (2));
System.out.println ("Gender=" +res.getstring (3));
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public void Connect1 (int id) {
Java.sql.Connection Con=null;
Java.sql.Statement Sta=null;
Java.sql.ResultSet Res=null;
Driver dri=new oracledriver ();
try {
Drivermanager.registerdriver (DRI);
Con=drivermanager.getconnection (URL, user, password);
Sta=con.createstatement ();
String sql= "SELECT * from Lanzhou where id=" +ID;
Res=sta.executequery (SQL);
while (Res.next ()) {
System.out.println ("id=" +res.getint (1) + "; "+" Name= "+res.getstring (2) +"; "+" gender= "+res.getstring (3));
System.out.println ("id=" +res.getint (1));
System.out.println ("Name=" +res.getstring (2));
System.out.println ("Gender=" +res.getstring (3));
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public static void Main (string[] args) {
Connection c=new Connection ();
C.connect ();
C.connect1 (1);
}
}

Steps for the Eclipse and database manual connection:

1. Create a. sql file

2. Open Perspective window, select Datebase development

3. Select the Date Source Explorer window and click on the icon with the Yellow plus sign (New connection profile)

4, then select the corresponding database "Oracle and so on", you can change the name to the name of the database

5. Enter driver settings option and click New Driver definition

6, set Name/type to 9 or 10; set add JDBC corresponding jar package (Ojdbc14.jar) in the database, and the properties setting sets the server behind @ to localhost and db to the name of the database (for example: XE);

Change the datebase name to your own user in the database, and change the password to the password of your own user.

7. Then click OK, enter user name to enter the password for your database user, and finally click Test Connection if ping successed! is displayed Indicates that the connection was successful. Finally select Savepassword, and click Finish.

8. Finally select the type in the new SQL file: Database type [Oracle9, 10];

Name: Names of databases [Oracle];

Datebase: The name of the database [XE].

This allows the database to be manipulated in eclipse, and SQL result displays the state and operation result of the action statement in SQL file.

JDBC Learning Note 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.