Programming for 3.1 JDBC
JDBC is Java database Programming API,JDBC provides a programming interface for the JA VA application to Access database resources, providing support for the SQL language. The primary role of JDBC is to connect the database, maintain the data source, implement the SQL language, and send SQL to the database and return and process the results. The following Computeraccess program demonstrates the process of using JDBC to access computer data tables in a MySQL database.
Package COM.JOY_CZ.JDBC;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.Properties;
/**
* @author Cai Zhe
*/
public class computeraccess{
public static void Main (string[] args) {
Connection conn = null;
try{
Load MySQL Database driver
Class.forName ("Org.gjt.mm.mysql.Driver");
Setting access Properties
Properties prop = new properties ();
Prop.setproperty ("User", "root");
Prop.setproperty ("Password", "123456");
Open a database connection and connect to the specified URL
con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/hibernate", prop);
}catch (ClassNotFoundException e) {
E.printstacktrace ();}
catch (SQLException e) {
E.printstacktrace ();}}}
3.2 Connecting the database DriverManager
The DriverManager in JDBC is used to manage the database driver. With DriverManager, you can establish database connections, manage multiple database drivers, and identify database drivers for different database URLs. Examples are as follows:
Package COM.JOY_CZ.JDBC;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.Properties;
/**
* @author Cai Zhe
*/
public class Drivermanagertest
{public static void main (String [] args)
{
Connection conn = null;
try {
Load MySQL Database driver
Class.forName ("Org.gjt.mm.mysql.Driver");
Class.forName ("Org.postgresql.Driver");
Setting access Properties
Properties prop = new properties ();
Prop.setproperty ("User", "root");
Prop.setproperty ("Password", "123456");
Output logs to the console
Drivermanager.setlogwriter (New PrintWriter (System.out));
Open a database connection and connect to the specified URL
String Mysqlurl = "Jdbc:mysql://localhost:3306/hibernate";
String Postgresqlurl = "Jdbc:postgresql://locahost:5432//hibernate";
con = drivermanager.getconnection (mysqlurl,prop);
con = drivermanager.getconnection (postgresqlurl,prop);
}catch (ClassNotFoundException e) {
E.printstacktrace ();}
catch (SQLException e) {
E.printstacktrace ();}}}
3.3 Data sources
The DataSource object represents the data source for Java. DataSource is a tool for database programming, and DataSource can be used to obtain database connections. DataSource itself can be registered to the Jndi naming server, and through Jndi, different Java applications can share the DataSource, thus providing great convenience for the development of the database program. Examples are as follows:
Package COM.JOY_CZ.JDBC;
Import java.sql.Connection;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import Com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
/**
* @author Cai Zhe
*/
public class Basicdatasourcetest
{public static void main (String [] args)
{
Mysqldatasource ds = new Mysqldatasource ();
Ds.seturl ("Jdbc:mysql://localhost:3306/hibernate");
Ds.setuser ("root");
Ds.setpassword ("123456");
Connection conn = Ds.getconnection ();
}
}
3.4 Database Connection Pool
(1) C3P0 Connection pool
You can provide a data source of type ConnectionPoolDataSource through a connection pool. Currently, there are many open source implementations for connection pooling, and there are a number of connection pool implementation tools that can be configured to hibernate, including the C3P0 Project (http://sourceforge.net/projects/c3p0/). The following code uses the Combopooleddatasource provided by C3P0
Package COM.JOY_CZ.JDBC;
Import java.sql.Connection;
Import Java.sql.ResultSet;
Import java.sql.Statement;
Import Com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* @author Cai Zhe
*/
public class C3p0test
{public static void main (String [] args)
{
Create a connection pool data source provided by C3P0
Combopooleddatasource CPDs = new Combopooleddatasource ();
Setting Up Database Drivers
Cpds.setdriverclass ("Org.gjt.mm.mysql.Driver");
Set the URL for JDBC
Cpds.seturl ("Jdbc:mysql://localhost:3306/hibernate");
Cpds.setuser ("root");
Cpds.setpassword ("123456");
Getting a database connection from a connection pool data source
Connection conn = Cpds.getconnection ();
}
}
(2) Pooleddatasource and Jndi
The datasource implemented with connection pooling is a resource that needs to be called repeatedly in a system and is typically registered to Jndi for use by other applications. The Name Service tool using the Jndi service is available from the Tomcat package (Naming-common.jar) and requires the Naming-common.jar to be imported into the classpath. Examples are as follows:
Package COM.JOY_CZ.JDBC;
Import java.sql.Connection;
Import Java.sql.ResultSet;
Import java.sql.Statement;
Import java.util.Hashtable;
Import Javax.naming.Context;
Import Javax.naming.InitialContext;
Import Javax.sql.DataSource;
Import Com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* @author Cai Zhe
*/
public class C3p0jnditest
{public static void main (String [] args)
{
Hashtable table = new Hashtable ();
Set the factory class for Jndi
Table.put (Context.initial_context_factory, "org.apache.naming.java.javaURLContextFactory");
Initializing the Jndi context
InitialContext CTX = new initialcontext (table);
Create a connection pool data source provided by C3P0
Combopooleddatasource CPDs = new Combopooleddatasource ();
Setting Up Database Drivers
Cpds.setdriverclass ("Org.gjt.mm.mysql.Driver");
Set the URL for JDBC
Cpds.seturl ("Jdbc:mysql://localhost:3306/hibernate");
Cpds.setuser ("root");
Cpds.setpassword ("123456");
Cpds.setinitialpoolsize (10);
Bind the data source to Jndi with the name "Hibernateds"
Ctx.bind ("Hibernateds", CPDs);
Finding a data source from Jndi
DataSource ds = (DataSource) ctx.lookup ("Hibernateds");
Getting a database connection from a connection pool data source
Connection conn = Ds.getconnection ();
}
}