Java connection to various database instances
1. oracle8/8i/9i Database (thin Mode)
Import java.net. url;
Import java. SQL .*;
Public class javaoracle ...{
Public javaoracle ()...{
}
Public static void main (string [] ARGs )...{
Try ...{
// Oracledriver exist check
Try ...{
Class. forname ("oracle. JDBC. Driver. oracledriver ");
}
Catch (Java. Lang. classnotfoundexception e )...{
System. Err. Print (E. getmessage ());
}
// Get connection
String url = "JDBC: oracle: thin :@ server: 1521: sdcdb ";
Connection conn = drivermanager. getconnection (URL, "test", "test ");
// Execute SQL
Statement stmt = conn. createstatement ();
Resultset rs1_stmt.exe cutequery ("select score from my ");
While (Rs. Next ())...{
System. Out. println (Rs. getstring (1 ));
}
Conn. Close ();
}
Catch (sqlexception ex)
While (ex! = NULL)
...{
System. Out. println (ex. getsqlstate ());
}
}
}
}
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 ();
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 );