In general, we will write this Class. froName ("") in Class. forName (""). newInstance (); what is the purpose of connecting newInstance () to the end? In fact, it is very simple: using Class. forName ("") alone is useless to dynamically load classes, and its ultimate goal is to instantiate objects. While Class. forName (""). newInstance () returns the object. If you have experience in database development, you may find that when we load the database driver package, some of them did not call the newInstance () method? That is to say, some jdbc statements to connect to the database are Class. forName (xxx. xx. xx); but there are some: Class. forName (xxx. xx. xx ). newInstance (). Why are these two methods used?
Class. forName (""); is used to require JVM to search for and load the specified Class. If there is a static initializer in the Class, JVM will inevitably execute the static code segment of the Class. In the JDBC specification, the Driver class must be registered with DriverManager, that is, the code of the Driver class of any JDBC Driver must be similar to the following:
1 public class MyJDBCDriver implements Driver {
2
3 static {
4
5 DriverManager. registerDriver (new MyJDBCDriver ());
6
7}
8
9}
10
Since the static initialization has been registered, we only need Class. forName (XXX. XXX); To use JDBC.
So good, understand the Class. forName (""), let's take a look at DriverManager. getConnection (url, user, pwd); what does this method mean? Take a notebook as an example. Our laptop has a battery and the machine has recognized the battery and loaded a certain amount of power. So good, now we can start the notebook, but after it is turned on, we need to do one more thing. What should we do? We have installed the operating system on our computer, so we need to choose the operating system, and the url is equivalent to the database we want to use. If the operating system is a multi-user operating system, we need to select the corresponding user, so a good user is equivalent to the database user name. As for pwd, it is very simple. Of course, it is the login password.
Now let's analyze the writing of the entire database connection class:
1 import java. SQL. Connection;
2 import java. SQL. DriverManager;
3 import java. SQL. SQLException;
4
5 /**
6 * database connection
7 *
8 * @ author funnyking
9 *
10 */
11 public class SqlConnetction {
12
13 /**
14 * database connection method (static)
15 *
16 * @ return Connection
17 * @ throws ClassNotFoundException
18 * @ throws SQLException
19 */
20 public static Connection getConnetction () throws SQLException {
21 Connection conn = null;
22 try {
23 // direct connection driver
24 Class. forName ("com. microsoft. jdbc. sqlserver. SQLServerDriver ");
25
26} catch (ClassNotFoundException e ){
27 // TODO Auto-generated catch block
28 System. out. println ("loading driver exception:" + e. getMessage ());
29}
30 // database connection address
31 String url = "jdbc: microsoft: sqlserver: // 127.0.0.1: 1433; DatabaseName = PM ";
32 // User Name
33 String user = "sa ";
34 // Password
35 String password = "";
36 conn = DriverManager. getConnection (url, user, password );
37
38 return conn;
39
40}
41
42 /**
43 * disable the Connection method (static)
44 *
45 */
46 public static void getCloseConnettion (Connection conn ){
47 try {
48 conn. close ();
49} catch (SQLException e ){
50 // TODO Auto-generated catch block
51 System. out. println ("An error occurred while closing the data connection:" + e. getMessage ());
52}
53}
54
55}
56
57
58
59
The basic writing method is like this. Well, the following shows the connection methods for different databases:
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 of the database
String user = "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 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 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 indicates 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 indicates the Database Name
Connection conn = DriverManager. getConnection (url );
7. PostgreSQL database
Class. forName ("org. postgresql. Driver"). newInstance ();
String url = "jdbc: postgresql: // localhost/myDB"
// MyDB indicates the Database Name
String user = "myuser ";
String password = "mypassword ";
Connection conn = DriverManager. getConnection (url, user, password );