Connection between java implementation and various types of databases (mysql, oracle, access, db2, sybase)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.