import java.sql.*;
import java.io.*;
import java.util.Properties;
import org.apache.commons.dbcp.*;
import org.apache.commons.pool.impl.*;
public class DbcpExample1 {
public static void main (String args []) throws Exception {
Class.forName("org.gjt.mm.mysql.Driver");
Properties p = new Properties();
String user = "urano";
String password = "urano398";
String database = "udb";
p.put("user", user);
p.put("password", password);
p.put("database", database);
GenericObjectPool pool = new GenericObjectPool(null);
DriverManagerConnectionFactory cf =
new DriverManagerConnectionFactory("jdbc:mysql://localhost:3306/" +
database + "?useUnicode=true&characterEncoding=SJIS", p);
PoolableConnectionFactory pcf =
new PoolableConnectionFactory(cf, pool, null, null, false, true);
PoolingDataSource ds = new PoolingDataSource(pool);
Connection conn = ds.getConnection();
System.out.println ("connected.");
// SQL文(DML文)を作ります。
Statement stmt = conn.createStatement ();
// SELECT文の場合はexecuteQuery()を実行します。
ResultSet rset
= stmt.executeQuery("SELECT SHITEN_CODE, SHITEN_NAME, EMPLOYEE_NUM FROM SHITEN");
while (rset.next())
System.out.println(rset.getString (1) + "\t" + rset.getString(2) + "\t"
+ rset.getInt(3));
System.out.println ("Your JDBC installation is correct.");
// 結果セットを解放
rset.close();
// SQL文を解放
stmt.close();
// データベースから切断して、完了。
conn.close();
}
}
// end.
|