Mysql:
String driver = "com. mysql. jdbc. Driver ";
String url = "jdbc: mysql: // 127.0.0.1: 3306/test ";
("Jdbc: mysql: // localhost: 3306/demo? Username = root & password = root
& UseUnicode = true & characterEncoding = UTF-8 ") (default Connection database
Username and password are both root)
String username = "root ";
String password = "mysql ";
Class. forName (driver );
Connection connection = DriverManager. getConnection (url, username, password );
Oracle:
String driver = "oracle. jdbc. driver. OracleDriver ";
String url = "jdbc: oracle: thin :@ localhost: 1521: ORCL ";
String username = "scott ";
String password = "tiger ";
Class. forName (driver );
Connection connection = DriverManager. getConnection (url, username, password );
Access:
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
Connection dbconn = DriverManager. getConnection ("jdbc: odbc: dbname ");
Access:
Private static String driver = "sun. jdbc. odbc. JdbcOdbcDriver"; // declare a driver String
Private static String url = "jdbc: odbc: driver = {Microsoft Access Driver (*. mdb)}; DBQ = d: // Java // db1.mdb ";
Private static Connection con = null; // declare database Connection object reference
Private static Statement stat = null; // declare a Statement object reference
Private static PreparedStatement psInsert = null; // declare precompiled statement object reference
Private static ResultSet rs = null; // declare a result set Object Reference
Public Access (){
GetConnection ();
}
Public static Connection getConnection () // obtain the database Connection method
{
Try
{
Class. forName (driver); // load the driver Class
Con = DriverManager. getConnection (url); // get the connection
System. out. println (con );
Stat = con. createStatement ();
Stat.exe cuteUpdate ("insert into c (tennumber, name) values (30, 'lilin ')");
ResultSet se = stat.exe cuteQuery ("select * from c ");
While (se. next ()){
System. out. println ("telnumber" + se. getString ("tennumber") + "\ t" + "name" + se. getString ("name "));
}
}
Catch (Exception e) {e. printStackTrace ();}
Return con; // return the connection
}
SQL Server
Class. forName ("com. microsoft. jdbc. sqlserver. SQLServerDriver ");
String url = "jdbc: microsoft: sqlserver: // localhost: 1433; DatabaseName = dbname ";
String password = "";
String name = "";
Connection connection = DriverManager. getConnection (url, name, password );
DB2:
Class. forName ("com. ibm. db2.jdbc. app. DB2Driver ");
String url = "jdbc: db2: // localhost: 5000/dbname ";
String name = "";
String password = "";
Connection connection = DriverManager. getConnection (url, name, password );
Sybase:
Class. forName ("com. sybase. jdbc. SybDriver"); (. newInstance () new instance
String url = "jdbc: sybase: Tds: localhost: 5007/dbname ";
Properties sysProps = System. getProperties ();
SysProps. put ("user ","");
SysProps. put ("password ","");
Connection connection = DriverManager. getConnection (url, sysProps );
This article is from the "java Notepad" blog, please be sure to keep this source http://6501542.blog.51cto.com/6491542/1287804