In this article, the editor will mainly introduce to you how Java connects to Oracle, DB2, Sql Server, Sybase, MySQL, PostgreSQL and other databases.
1. Oracle Database
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl is the SID of the database String u ser="test" ; 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 is your database name String user : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : ="admin"; String password=""; Connection conn= DriverManager.getConnection(url,user,password);3. Sql Server Database
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); String url="jdbc:microsoft:sqlserver://localhost:1433;Databa seName=mydb"; //mydb is the 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 sy sProps = System.getProperties (); SysProps.put("user","userid"); SysProps.put("password","user_password"); Connection conn= DriverManager.getConnection(url, SysProps );5. MySQL database
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&us eUnicode=true&characterEncoding=8859_1" //myDB is the database Connection conn= DriverManager.getConnection(url);6. PostgreSQL database
Class.forName("org.postgresql.Driver").newInstance(); String url ="jdbc:postgresql://localhost/myDB" //myDB is the database name String user="myuser"; String g password="mypassword" ; Connection conn= DriverManager.getConnection(url,user,password);The above are the tips for connecting Java to various databases. I believe it will be helpful to everyone to implement Java to connect to databases.