Code for connecting various databases in Java and additional instructions

Source: Internet
Author: User
Tags db2 informix mysql postgresql sybase sybase database mysql database
Data | database


added: In the actual project development process, it is necessary to put the JDBC driver package (Xxx.jar) of various databases under the project's Web module in the Lib folder (recommended) or under the application server Common/lib. Both locations will be automatically searched by the server. It is not recommended to find these packages by modifying the environment variable CLASSPATH. Because in the actual project development of course is less to modify the project outside the better configuration. It's a lot easier to maintain.





the code in this article mainly lists the key code to connect to the database, and other access to the database code is omitted. The main part is also found on the Internet some information, not original.





1, oracle8/8i/9i database (thin mode) class.forname ("Oracle.jdbc.driver.OracleDriver"). newinstance (); String url= "Jdbc:oracle:thin: @localhost: 1521:ORCL"; ORCL the SID String user= "test" for the database; 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 for 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 for 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 is 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 is the database name Connection conn= drivermanager.getconnection (URL); 7, PostgreSQL database Class.forName ("Org.postgresql.Driver"). newinstance (); String url = "Jdbc:postgresql://localhost/mydb"//mydb the database name string user= "MyUser"; String password= "MyPassword"; Connection conn= drivermanager.getconnection (Url,user,password);








Related Article

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.