Claim the log 7-JDBC summary.

xiaoxiao2021-03-30  202

Use JDBC to access the database, first make sure the selected database is installed and running, and the driver is available. (You can download JDBC driver from http://industry.java.sun.com/products/jdbc/drivers) Java and database interaction usually consists of steps: 1. Load Database Driver (JDBC driver or JDBC- ODBC bridge). 2. Create a database of Connection. 3. Create a Statement object. This object actually executes SQL or stored procedures. 4. Create a ResultSet and populate the result of the execution query (if the target is retrieved or directly updated). 5. Retrieve or update data from ResultSet.

First, instantiate the drive

Access the database, first load the JDBC driver, and then create the connection to the driver corresponding to the database by DRIVERMANAGER to determine. Use class.forname () to directly load, register with DRIVERMANAGER: public class bricing extends Object {

Public static void main (string args []) {

String drivername = "jdata2_0.sql. $ Driver";

Try {class.Forname (DRIVERNAME);} catch (classnotfoundexception e) {system.out.println ("Error Creating Class:" E.getMessage ());}}}

Second, create Connection

Example: import .sql.connection; import .sql.driverManager; import .sql.sqlexception;

public class Pricing extends Object {public static void main (String args []) {String driverName = "JData2_0.sql $ Driver."; String connectURL = "jdbc: JDataConnect: //127.0.0.1/pricing"; Connection conn = null ; try {Class.forName (driverName); conn = DriverManager.getConnection (connectURL);} catch (ClassNotFoundException e) {System.out.println ( "Error creating class:" e.getMessage ());} catch (SQLException e) {system.out.println ("Error Creating Connection:" E.GetMessage ());} finally {system.out.println ("Closing Connections ..."); try {conn.close ();} Catch (SQLEXCEPTION E) {system.out.println ("can't Close Connection.");}}}} All kinds of databases use JDBC connections // db2string drivername = "com.ibm.db2.jcc.db2driver" String connectURL = "JDBC: DB2: // localhost: 5000 / sample"; class.forname (drivername); connection conn = drivermanager.getConnection (connectURL, "User", "Password");

// Oracle ("Oracle.jdbc.driver.OracleDriver"). NewInstance (); string url = "jdbc: Oracle: Thin: @localhost: 1521: Orcl; string user =" test " String password = "test"; connection conn = drivermanager.getConnection (URL, User, Password);

//Sqlclass.Forname ("com.microsoft.jdbc.sqlserver.sqlserverdriver "). Newinstance (); string url =" JDBC: Microsoft: SQLServer: // localhost: 1433; databasename = mydb; string user = "sa" String password = ""; connection conn = drivermanager.getConnection (URL, User, Password);

//SybaseClass.forName("com.sybase.jdbc.SybDriver ".) NewInstance (); String url =" jdbc: sybase: Tds: localhost: 5007 / myDB "; Properties sysProps = System.getProperties (); SysProps.put ("User", "UserID"); sysprops.put ("password", "user_password"); connection conn = drivermanager.getConnection (URL, sysprops); // mysqlclass.forname ("Org.gjt.mm.mysql. Driver ".) newInstance (); String url =" jdbc: mysql: // localhost / myDB user = soft & password = soft1234 & useUnicode = true & characterEncoding = 8859_1 "Connection conn = DriverManager.getConnection (url);?

//AccessClass.Forname ("Sun.jdbc.odbc.jdbcodbcdriver"); string url = "JDBC: ODBC: driver = {Microsoft Access Driver (*. MDB)}; dbq =" Application.getRealPath ("/ DATA / MYDB.MDB "); connection conn = drivermanager.getConnection (URL,", ""); statement stmtnew = conn.createstatement ();

Third, create a Statement object

The Statement object is used to send SQL statements to the database. There are three statement objects that are all packed containers that perform SQL statements on a given connection: Statement, PreparedStatement (it comes from Statement) and CallableStatement (it is inherited from PreparedStatement). They are all dedicated to send specific types of SQL statements: Statement objects are used to perform simple SQL statements without parameters; PreparedStatement objects are used to perform a precompiled SQL statement that belts or does not have an in-parameter (when performing a particular query PreparedStatement may be most useful); CallableStatement object is used to perform calls for database stored procedures. A difference between CallableStatement and PreparedStatement is: In addition to the usually created ResultSet, CallableStatement can also provide OUT parameters

STATEMENT instance: Add import .sql.Statement in the program; after the program generates the Connection object, use the object to generate the statement object statement statement = null; try {statement = conn.createstatement ();} catch (sqlexception e) {system. Out.println ("SQL Error:" E.GetMessage ());} preparedStatement instance ... statement = conn.preparestatement ("SELECT * from test where" "ID ?"); statement. Setint (1, 5); Statement.setint (2, 10); ResultSet = statement.executeQuery (); ... CallableStatement instance

Fourth, execute Statement

The STATEMENT interface provides three ways to perform SQL statements: ExecuteQuery, ExecuteUpdate, and Execute. Which method is used by the content generated by the SQL statement.

Method EXECUTEQUERY is used to generate a single result set statement, such as a SELECT statement. Methods EXECUTEUPDATE are used to perform INSERT, UPDATE, or DELETE statements, and SQL DDL (data definition language) statements, such as Create Table, and Drop Table. The effect of INSERT, UPDATE or DELETE statement is to modify a column or multiple columns in a table in a table. The return value of ExecuteUpdate is an integer that indicates the number of rows affected (ie update counts). For CREATE TABLE or DROP TABLE, the return value of ExecuteUpdate is always zero. Methods EXECUTE is used to perform statements that return multiple result sets, multiple update counts, or two combinations.

Example: import.Sql.resultset; ResultSet RS = NULL; RS = Statement.executeQuery ("Select * from test") after generating statement;

Five, RESULTSET

After the resultset is created, it has a "pointer" that references the relative position within the data set. After returning after the ResultSet statement (even if the table is empty), the pointer is just in the "above" of the first line. To reach the first line of the actual data, the application calls the next () method. This method returns a boolean value indicating whether there is a line at the new location. If data is not found, NEXT () returns false. Getxxx () and Wasnull () ResultSet.Getxxx method Gets a common JDBC data type to the very large row value Use the stream GetBinaryStream to return only the database original bytes without any conversion stream. GetasciistReam returns a stream that provides a single-byte ASCII character. GetUnicODestream returns a stream that provides a double-byte Unicode character.

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

New Post(0)