Java connects to various database codes and drivers

Source: Internet
Author: User
1. This section describes how to connect java code to common databases, including MySQL, Sybase, Oracle, and SqlServer. To connect to a database, you must add a jar package to the project to load the driver, I have sorted these jar packages into csdn resources: download. csdn. netdetailwangduanqing594570040932,

1. This section describes how to connect java code to common databases, including MySQL, Sybase, Oracle, and SqlServer. To connect to a database, you must add a jar package to the project to load the driver, these jar packages I organized in the csdn resources,: http://download.csdn.net/detail/wangduanqing5945/7004093 2,

1. This section describes how to connect java code to common databases, including MySQL, Sybase, Oracle, and SqlServer. To connect to a database, you must add a jar package to the project to load the driver, I have sorted these jar packages into csdn resources: http://download.csdn.net/detail/wangduanqing5945/700409321_mysqlconnection
Connection connection = null;try {Class.forName("com.mysql.jdbc.Driver");connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/boxmanager", "username", "password");} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}

Here, I will explain to some friends who have never used it. com. mysql. jdbc. Driver is the package name and Class Name of the Driver class in MySQL. The following getConnection method has three parameters: the first database address corresponds to a database, the second parameter is the database user name, and the third parameter is the database password. 3. Sybase connection
Try {String driver = "com. sybase. jdbc3.jdbc. sybDriver "; Class. forName (driver ). newInstance (); String url = "jdbc: sybase: Tds: 192.168.100.252: 4500/myDB"; // myDB your database name Properties sysProps = System. getProperties (); sysProps. put ("user", username); // sets the database access username sysProps. put ("password", password); // password connection = DriverManager. getConnection (url, sysProps);} catch (Exception e) {e. printStackTrace ();}

I must have understood the above explanation. I don't need to explain it here. The ideas are consistent. 4. Oracle connection
// Obtain the Connection ct = null; try {// load the driver Class. forName ("oracle. jdbc. driver. oracleDriver "); ct = DriverManager. getConnection ("jdbc: oracle: thin: @ 127.0.0.1: 1521: simlink", "your username", "your pwd");} catch (ClassNotFoundException e) {e. printStackTrace ();} catch (SQLException e) {e. printStackTrace ();}

5. SQL server connection
String driverName = "com. microsoft. jdbc. sqlserver. SQLServerDriver "; String dbURL =" jdbc: microsoft: sqlserver: // localhost: 1433; databasename = li "; // 1433 indicates the database port, "li" is your database name String userName = "sa"; // sa is the super user of the database. It is best not to change the name. Permission problem String userPwd = "123456 "; // sa password Connection dbConn = null; try {Class. forName (driverName ). newInstance (); dbConn = DriverManager. getConnection (dbURL, userName, userPwd); System. out . Println ("connection successful! ");} Catch (Exception e) {e. printStackTrace ();}



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.