JDBC related knowledge

xiaoxiao2021-03-06  65

First, establish a connection with data

(1) Load the appropriate JDBC driver

Try

{

// Register the database driver for Oracle driver

Class.Forname ("Oracle.jdbc.driver.OracleDriver");

}

Catch (java.lang.classnotfoundexception e)

{

// This is to make it easy to debug the program, and the error prints MYDB () will know where it is wrong.

System.err.println ("MyDB (): E.GetMessage ());

}

Use class.Forname to explicitly load JDBC driver

(2) Establish connection with the database

Try

{

Conn = drivermanager.getConnection

"JDBC: Oracle: Thin: @ 127.0.0.1: 1521: ORC8i", "Java", "88888888");

}

Catch (SQLException EX)

{

System.err.Println ("Conn:" ex.getMessage ());

}

Java.sql.driverManager is a basic class that manages JDBC driver in Java.

AS Part of Its Initialization, The DriverManager Class Will Attempt to Load The Driver Classes Reference in The "JDBC.DRIVERS" System Property

(DRIVERMANAGER class will load the JDBC driver defined in the jdbc.drivers attribute during initialization)

A Program CAN Also Explicitly Load JDBC Drivers At Any Time. For example, the my.sql.driver is loaded with the Following Statement: Example:

Class.Forname ("Oracle.jdbc.driver.OracleDriver");

DRIVERMANAGER will load proper drivers when calling a getConnection method

If your loading driver identifies the JDBC URL provided to DRIVERMANAGER.GETCONNECTION, the driver will establish a connection to the specified DBMS based on the JDBC URL. As the name is indicated, the DriverManager class will manage all the details of the connection to you after the scene. Unless you are a write driver, you may not need any other ways of this class, the unique way that general programmers need to use directly in this class is DriverManager.getConnection.

The DriverManager.getConnection method returns an open connection, you can use this connection to create JDBC Statements and send SQL statements to the database. In the previous example, the conn object is an open connection.

Second, use the database connection that has been created (implemented by the Statement class)

(1) Creating a Statement object

Statement Stmt = conn.createstatement ();

(2) Description of the java.sql.statement interface

The Object Used for Executing A Static SQL Statement and Returning The Results It Products

The object is used to perform a static SQL script and return the result of the process.

By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved (interleaved) with the reading of another, each must have been generated by different Statement objects. All Execution Methods in the Statement Interface Implicitly Close A Starative's Current ResultSet Object if An Open ONE EXISTS.

By default, each Statement object can only open a ResultSet object at the same time.

All methods of Statement Interface are executed, if there is an activity RSESulset object, implicitly close a resultset object.

Two methods:

ExecuteQuery (String SQL)

Executes The Given Sql Statement, Which Returns A Single ResultSet Object.

ExecuteUpdate (String SQL)

Executes The Given Sql Statement, Which May Be An Insert, Update, or Delete Statement OR An Sql Statement That Returns Nothing, Such as an SQL DDL Statement.

(3) java.sql.resultset interface description

A Table of Data Representing A Database Result Set, Which is usually generated by Executing a statement That queries the database.

A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object , IT can be used in a while loop to iterate through the result set.

A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row. It is possible to produce ResultSet objects that are scrollable and / or updatable . The following code fragment, in which con is a valid Connection object, illustrates how to make a result set that is scrollable and insensitive to updates by others, and that is updatable. See ResultSet fields for other options.Statement stmt = con.createStatement (

ResultSet.Type_Scroll_InSensitive,

ResultSet.concur_updatable;

ResultSet RS = Stmt.executeQuery ("SELECT A, B from Table2");

// rs Will Be Scrollable, Will Not Show Changes Made by tasers,

// and will be updata

You can use the While cycle statement to remove the corresponding value of the ResultSet object:

While (rs.next ())

{

System.out.println ("UserName:" rs.getstring (1));

System.out.println ("Password:" rs.getstring (2));

System.out.println ("email:" rs.getstring (3));

}

(4) Application example

Package counter;

Import java.net. *;

Import java.sql. *;

Import java.lang. *;

Import java.io. *;

Import java.util. *;

Import oracle.jdbc.driver. *;

Public Class DB

{

Connection conn = null; // Database connection

ResultSet RS = NULL; // Recording Set

String username = ""; // username

String password = ""; // password

String email = ""; // email

String homepage = ""; // home page

String Signs = ""; // Signature

// DB builder

Public db ()

{

Try

{

// Register the database driver for Oracle driver

Class.Forname ("Oracle.jdbc.driver.OracleDriver");

}

Catch (java.lang.classnotfoundexception e)

{

// This is to make it easy to debug the program, and the error prints MYDB () will know where it is wrong.

System.err.println ("MyDB (): E.GetMessage ());

}

}

Public ResultSet ExecuteQuery (String SQL) {

RS = NULL;

Try

{

/ / Establish a database connection, use the "one of the Thin connection method, DEMO is the host name, DEMODB is the database, the two DEMOs later are user names and passwords

Conn = drivermanager.getConnection

"JDBC: Oracle: Thin: @ 127.0.0.1: 1521: ORC8i", "Java", "88888888");

Statement Stmt = conn.createstatement ();

/ / Execute Database Query Operation

RS = stmt.executequery (SQL);

}

Catch (SQLException EX)

{

System.err.println ("DB.ExecuteQuery: ex.getMessage ());

}

Return RS;

}

Public Boolean ExecuteUpdate (STRING SQL)

{

Boolean bupdate = false;

RS = NULL;

Try

{

Conn = drivermanager.getConnection

"JDBC: Oracle: Thin: @ 127.0.0.1: 1521: ORC8i", "Java", "88888888");

Statement Stmt = conn.createstatement ();

Int rowcount = stmt.executeUpdate (SQL);

// If you are not successful, Bupdate will return 0

IF (RowCount! = 0)

BUPDATE = TRUE;

}

Catch (SQLException EX)

{

// Print an error message

System.err.println ("DB.ExecuteUpdate:" ex.getMessage ());

}

Return BUPDATE;

}

转载请注明原文地址:https://www.9cbs.com/read-87629.html

New Post(0)