Various databases and JDBC configuration in the configuration of struts and hibernate

2010-12-14  来源:本站原创  分类:Database  人气:108 

1, Oracle8/8i/9i database (thins mode)

Class.forName ("oracle.jdbc.driver.OracleDriver"). NewInstance ();
String url = "jdbc: oracle: thin: @ localhost: 1521: orcl"; / / orcl SID for the database
String user = "test";
String password = "test";
Connection conn = DriverManager.getConnection (url, user, password);

2, DB2 database

Class.forName ("com.ibm.db2.jdbc.app.DB2Driver"). NewInstance ();
String url = "jdbc: db2: / / localhost: 5000/sample"; / / sample for your database name
String user = "admin";
String password = "";
Connection conn = DriverManager.getConnection (url, user, password);

3, Sql Server7.0/2000 database

Class.forName ("com.microsoft.jdbc.sqlserver.SQLServerDriver"). NewInstance ();
String url = "jdbc: microsoft: sqlserver: / / localhost: 1433; DatabaseName = mydb";
/ / Mydb database
String user = "sa";
String password = "";
Connection conn = DriverManager.getConnection (url, user, password);

4, Sybase Database

Class.forName ("com.sybase.jdbc.SybDriver"). NewInstance ();
String url = "jdbc: sybase: Tds: localhost: 5007/myDB"; / / myDB name for your database
Properties sysProps = System.getProperties ();
SysProps.put ("user", "userid");
SysProps.put ("password", "user_password");
Connection conn = DriverManager.getConnection (url, SysProps);

5, Informix Database

Class.forName ("com.informix.jdbc.IfxDriver"). NewInstance ();
String url = "jdbc: informix-sqli: / / 123.45.67.89:1533 / myDB: INFORMIXSERVER = myserver;
user = testuser; password = testpassword "; / / myDB name for the database
Connection conn = DriverManager.getConnection (url);

6, MySQL database

Class.forName ("org.gjt.mm.mysql.Driver"). NewInstance ();
String url = "jdbc: mysql: / / localhost: 3306/test"
/ / Test for the database name
Connection conn = DriverManager.getConnection (url);

7, PostgreSQL Database

Class.forName ("org.postgresql.Driver"). NewInstance ();
String url = "jdbc: postgresql: / / localhost / myDB" / / myDB name for the database
String user = "myuser";
String password = "mypassword";
Connection conn = DriverManager.getConnection (url, user, password);

8, access database, ODBC-Direct Used

Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc: odbc: Driver = {MicroSoft Access Driver (*. mdb)}; DBQ =" + application.getRealPath ("/ Data / ReportDemo.mdb");
Connection conn = DriverManager.getConnection (url ,"","");
Statement stmtNew = conn.createStatement ();

The following small with about ... by other struts_config.xml modify it. We Mysql Example

<data-sources>
<data-source key="infomgr_ds" type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="autoCommit" value="false" />
<set-property property="description" value="DB Connection Pool" />

<! - The other two connections
<set-property property="driverClass" value="org.gjt.mm.mysql.Driver" />

<set-property property="driverClass" value="sun.jdbc.odbc.JdbcOdbcDriver" />
->
<set-property property="driverClassName" value="com.mysql.jdbc.Driver" />
<set-property property="maxCount" value="15" />
<set-property property="minCount" value="5" />
<set-property property="password" value="root" />
<set-property property="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&charactorEncoding=gbk" />
<set-property property="username" value="root" />
<! - When using odbc
<set-property property="user" value="" />
<set-property property="password" value="" />
<set-property property="url" value="jdbc:odbc:infomgr" />
-> </ Data-source>
</ Data-sources>

Click to add this back to hibernate database connection configuration as an example we orcale named hibernate.cfg.xml, only about what

<hibernate-configuration>

<session-factory>

<property name="dialect"> net.sf.hibernate.dialect.Oracle9Dialect </ property>
<property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property>
<property name="connection.username"> j2eelwd </ property>
<property name="connection.password"> j2eelwd </ property>
<property name="connection.url"> jdbc: oracle: thin: @ localhost: 1521: King </ property>
<property name="connection.pool.size"> 1 </ property>
<property name="statement_cache.size"> 25 </ property>
<property name="jdbc.fetch_size"> 50 </ property>
<property name="jdbc.batch_size"> 30 </ property>
<property name="show_sql"> true </ property>
</ Session-factory>

</ Hibernate-configuration>

Casually talk about the various property elements: (master, skip the yo .. little brother is one of two ..)

dialect: the control used Oracle9
connection.driver_class: Oracle's JDBC driver class name
connection.username: Oracle database access user name
connection.password: Oracle database access passwords
connection.url: Oracle Database Access URL
connection.pool.size: database connection pool size
statement_cache.size: JDBC statement cache size
jdbc.fetch_size: JDBC's Statement to set the time to read data from the database each time the number of records
jdbc.batch_size: Set the database batch delete, batch updates and bulk insert batch size when
show_sql: Sets whether to display the console of the SQL statement submitted to the database, in the development of more useful when debugging

相关文章