Thursday, September 2, 2010

Java basic process of database operations

 1.Java a. Database connection 1.Drivermanager link database String className, url, uid, pwd; className = \ 'Oracle.jdbc.driver.OracleDriver \'; uid = \ 'scott \'; pwd = \ ' tiger \';...
1.Java
a. 1.Drivermanager link database database connection

String className, url, uid, pwd;

className = \ "Oracle.jdbc.driver.OracleDriver \";

uid = \ "scott \";

pwd = \ "tiger \";



url = \ "jdbc: oracle: thin: @ localhost: 1521: ora92 \";

Class.forName (classname);

Connection conn = DriverManager.getConnection (url, uid, pwd);

2.JNDI Links Database

String jndi = \ "jdbc / db \"; / / e20-040 9L0-609 data source name

/ / Context is a set of binding names to objects composed of

Hashtable env = new Hashtable ();

Context ctx = (Context) new InitialContext.lookup (\ "env \ ");// context where access to the data source object point

DataSource ds = (DataSource) ctx.lookup (jndi); / / find the data source

Connection conn = ds.getConnection ();//

b. implementation of the sql statement

String sql;

StateMent stat = conn.createStatement ();

ResultSet rs = stat.executeQuery (sql); / / implementation of the data query (select);

stat.executeUpdate (sql); / / implementation of data update statements (inset into, delete, update, drop)

stat.close ();

c. to execute sql statements with preparedStatement

String sql = \ "inset into table (id, name) values (?,?) \";

PreparedStatement ps = conn.prepareStatement (sql);

ps.setInt (1,001);

ps.setString (2, \ "zhangmanli \");

ps.executeQuery ();

int count = ps.executeUpdate ();

d. address the implementation of the results

Query, ResultSet object to return recordset

Update statement, return number, said the impact of the number of records updated

javax.sql .*

javax.naming .*;

Data processing:

A close connection to automatically submit

conn.setAutoCommit (false);

2 implementation of a series of sql statements

Statement sm;

sm = conn.createStatement (sql);

sm.executeUpdate ();

sm.close ();

3. Submit:

conn.commit ();

4. Rollback mechanism;

conn.rollback ();

e: Threading:

D: jndi and dataSource to get the database link:

import java.sql.ResultSet;

import java.sql .*;

import javax.sql.DataSource;

import javax.naming.Context;

import javax.naming.InitialContext;

import java.util.Hashtable;

import java.util.Properties;

import java.io. *;

public class BasicExample (

public static void main (String args []) (

Connection conn = null;

try (

Properties prop = new Properties ();

prop.load (new FileInputStream (\ "simple.properties \"));

Hashtable env = new Hashtable ();

env.put (Context.INITIAL_CONTEXT_FACTORY, prop.getProperty (\ "INITIAL_CONTEXT_FACTORY \"));

env.put (Context.PROVIDER_URL, prop.getProperty (\ "PROVIDER_URL \"));

InitialContext ctx = new InitialContext (env);

DataSource ds = (DataSource) ctx.lookup (\ "Book \");

Conn = ds.getConnection ();

Statement stat = conn.createStatement ();;

ResultSet rs = stmt.executeQuery (sql);

while (rs.next ()) (

int id = Integer.parseInt (rs.getString (\ "userId \"));

String userName = rs.getString (\ "username \");

)

) Catch (SQLException e) (

e.printStackTrace ();

) Finally (

try (

if (conn! = null) (

conn.close ();

)

) Catch (SQLException e) (

e.printStackTrace ();

)

)

)

);

No comments:

Post a Comment