Database access almost every little molding process to use the knowledge, how efficient access to the database is also a focus of our study, today's task is to summarize the Java access to the database method and about Api,java access to the database is the main method is JDBC, It is an application interface in the Java language that regulates how a client program accesses a database, provides methods such as querying and updating data in a database, and we summarize the JDBC
A: Java access to the database specific steps:
1 loading (registering) the database
Driver loading is the Access database provided by each database API to load into our program, load the JDBC driver, and register it to DriverManager, each database provides a different database driver, load the driver to add the jar package to the Lib folder, The following is a look at some of the main database JDBC driver registration code:
Oracle8/8i/9io database (thin mode)
Class.forName ("Oracle.jdbc.driver.OracleDriver"). newinstance ();
SQL server7.0/2000 Database Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver");
SQL server2005/2008 Database Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");
DB2 Database
Class.froname ("Com.ibm.db2.jdbc.app.DB2Driver"). newinstance ();
MySQL database class.forname ("Com.mysql.jdbc.Driver"). newinstance ();
PostgreSQL database Class.forName ("Com.postgresql.jdbc.Driver"). newinstance ();
2 Establish links
Establishing a connection between the databases is a necessary condition for accessing the database, just like water transfer in the north-South diversion, to be transferred first by the river to communicate. Establishing a connection is not the same for different databases, so let's look at some major databases to establish a database connection and get connection objects in different ways:
oracle8/8i/9i database (thin mode)
String url= "Jdbc:oracle:thin: @localhost: 1521:ORCL";
String user= "Scott";
String password= "Tiger";
Connection conn=drivermanager.getconnection (Url,user,password);
SQL server7.0/2000/2005/2008 Database
String url= "Jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs";
String user= "SA";
String password= "";
Connection conn=drivermanager.getconnection (Url,user,password);
DB2 Database
String url= "Jdbc:db2://localhost:5000/sample";
String user= "Amdin"
String password=-"";
Connection conn=drivermanager.getconnection (Url,user,password);
MySQL Database
String url= "jdbc:mysql://localhost:3306/testdb?user=root&password=root&useunicode=true& characterencoding=gb2312 ";
Connection conn=drivermanager.getconnection (URL);
PostgreSQL database
String url= "Jdbc:postgresql://localhost:5432/postgres";
String user= "Postgres"
String password= "Postgres";
Connection conn=drivermanager.getconnection (Url,user,password);
3. Execute SQL statements
Once the database connection is established, the next step is to prepare and execute the SQL statements, and the preparation is to build the statement object PreparedStatement object, for example:
Creating statement objects
Statement stmt=conn.createstatement ();
Creating PreparedStatement Objects
String sql= "SELECT * from user where username=? and password=? ";
PreparedStatement pstmt=conn.preparestatement (SQL);
Pstmt.setstring (1, "admin");
Pstmt.setstring (2, "Liubin");
After you are ready to execute the SQL statement, execute the SQL statement:
String sql= "SELECT * from users";
ResultSet rs=stmt.executequery (SQL);
Execute a dynamic SQL query
ResultSet Rs=pstmt.executequery ();
Execute the INSERT Update DELETE statement and define the SQL
Stmt.executeupdate (SQL);
4 Working with result sets
Access the result recordset ResultSet object. For example:
while (Rs.next)
{
Out.println ("Your first field content is:" +rs.getstring ("Name"));
Out.println ("Your second field content is:" +rs.getstring (2));
}
5 Closing the database
Close the resultset, Statement, PreparedStatement, and connection objects in turn to release the resources that are occupied. For example:
Rs.close ();
Stmt.clost ();
Pstmt.close ();
Con.close ();
Two: JDBC Transaction
What is a transaction:
First of all, talk about what matters. I think a transaction is a set of actions that manipulate the database.
Transaction is one of the core concepts in modern database theory. If a set of processing steps or all occurs or is not executed in one step, we call the group processing step a transaction. When all the steps are executed like an operation, we call the transaction to be committed. The transaction must be rolled back to the original system state because one or more of the steps failed, resulting in no step being committed.
Transactions must be subject to the ACID principles established by ISO/IEC. Acid is an abbreviation for atomicity (atomicity), consistency (consistency), isolation (isolation), and persistence (durability). The atomicity of a transaction indicates that any failure during the execution of the transaction will invalidate any modifications made by the firm. Consistency means that when a transaction fails, all data affected by the transaction should revert to the state it was before the transaction was executed. Isolation represents the modification of data during the execution of a transaction and is not visible to other transactions before the transaction commits. Persistence indicates that updates to committed transactions cannot be lost when a system or media failure occurs. Persistence is ensured through database backup and recovery.
The JDBC transaction is controlled with the Connection object. The JDBC Connection Interface (java.sql.Connection) provides two transaction modes: Autocommit and Manual commit. Java.sql.Connection provides the following methods of controlling transactions:
public void Setautocommit (Boolean)
public boolean getautocommit ()
public void commit ()
public void rollback ()
When using JDBC transaction demarcation, you can combine multiple SQL statements into a single transaction. One drawback of JDBC transactions is that the scope of a transaction is limited to a single database connection. A JDBC transaction cannot span multiple databases.
"Reprint" Java Database operations