What are the various writing methods of JDBC connecting to database in J2EE technology?
Today, I will talk to you about the various writing methods of JDBC connection database in J2EE technology, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
The following is how to write JDBC to connect to various databases, dedicated to Java beginners, give some suggestions, remember to import the corresponding driver jar package.
1. Oracle8/8i/9i database (thin mode)
Class.forName ("oracle.jdbc.driver.OracleDriver") .newInstance (); String url= "jdbc:oracle:thin:@localhost:1521:orcl"; / / orcl is the SID String user= "test" of 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 is 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 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 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)
Or
Class.forName ("com.mysql.jdbc.Driver"); String url= "jdbc:mysql://localhost/myDB? ser=root&password=system"; conn = DriverManager.getConnection (url)
7. PostgreSQL database
Class.forName ("org.postgresql.Driver") .newInstance (); String url = "jdbc:postgresql://localhost/myDB" / / myDB is the database name String user= "myuser"; String password= "mypassword"; Connection conn= DriverManager.getConnection (url,user,password)
Supplement: Sql Server2005 database
Class.forName ("com.microsoft.jdbc.sqlserver.SQLServerDriver") .newInstance (); String url= "jdbc:sqlserver://localhost:1433;DatabaseName=mydb"; / / mydb is the database String user= "sa"; String password= ""; Connection conn= DriverManager.getConnection (url,user,password)
It is a little different from the connection url of sql2000. These are the basic ways for JDBC to connect to the database.
After reading the above, do you have any further understanding of the various ways of writing JDBC connection database in J2EE technology? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.