Introduction to using Java programs to connect to various databases
1. Oracle8/8i/9i Database (thin Mode)
Class. forName ("oracle. jdbc. driver. OracleDriver"). newInstance ();
String url = "jdbc: oracle: thin: @ localhost: 1521: orcl"; // orcl indicates the SID of 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 is 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 is a 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 is your database name
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 indicates the database name.
Connection conn = DriverManager. getConnection (url );
6. MySQL database
Class. forName ("org. gjt. mm. mysql. Driver"). newInstance ();
// Or Class. forName ("com. mysql. jdbc. Driver ");
String url = "jdbc: mysql: // localhost/myDB?
User = soft & password = soft1234 & useUnicode = true & characterEncoding = 8859_1"
// MyDB indicates the Database Name
Connection conn = DriverManager. getConnection (url );
7. PostgreSQL database
Class. forName ("org. postgresql. Driver"). newInstance ();
String url = "jdbc: postgresql: // localhost/myDB" // myDB indicates the database name.
String user = "myuser ";
String password = "mypassword ";
Connection conn = DriverManager. getConnection (url, user, password );
8. access database directly connected to ODBC
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 ();