Java-load database Driver, get database connection, java-database connection
To perform database operations in Java, the two most important steps are to load the data driver and then connect to the database.
1. Load the database Driver (Class. forName (String className )):
Because Java is a pure object-oriented programming language, everything can be viewed as a class or class object, and the database is also. Java abstracts the database into an object through JDBC, then, perform operations on the object. The difference is that the database exists and does not need to be created in Java. All we need to do is to access a normal running database instance in our Java program.
However, a drive is required for normal operation of any class in Java. The general class drive is the JVM itself, and the database program is a program independent of the Virtual Machine driver, so you must implement the drive by yourself, at this time, the Class is used. forName (String className), which is used to write the drive. Then there is another question: what is the class name?
The external program must be accessed by Java. First, the external program must have interfaces called by Java and class names that comply with Java Naming rules. The Java interface is the Java drive controlled by the JVM, that is, JDBC. This interface is provided by the database vendor, so the class name is also provided by the database vendor, like MySQL named com. SQL. jdbc. Driver.
Loading principle of forName:
A. After the program runs, the class name and handle of the interface will be recorded in the process information of the program;
B. after forName is passed in, a thread with the class name will be found in the operating system, and the corresponding thread will find the drive corresponding to the thread, and then load the drive into JVM;
C. Then you can call the function of the process through the class name in the Java program.
Of course, if Eclipse is used, you must first load the JDBC driver into the runtime environment.
2. Get the database connection (DriverManager. getConnection (URL, USERNAME, PASSWORD )):
After loading forName, you will find that it is a database-class driver and then perform some special operations. JDBC uses the DriverManager class to manage database drivers, and DriverManager is only used to manage JDBC drivers. The loaded data driver is abstracted as a Java type and stored in the static variable driver of DriverManager. JDBC requires that a Java Process can only have one JDBC driver, and database access is the same as that of HTTP, so a connection must be established first.
Then, the static synchronization method of DriverManager getConnection uses the driver to establish a connection with the database. The first parameter URL of this method must also be referred to the database vendor's regulations. MySQL is written as jdbc: mysql: // hostname: port/specific database name. The second and third parameters are the database username and password. The return value of this method is a Connection object through which Java programs operate on the database.