----JDBC (Java database Connectivity) is the interface specification for Java and databases, and JDBC defines a generic low-level application programming Interface (API) that supports standard SQL functionality, which consists of classes and interfaces written in the Java language. Designed to allow database developers to provide standard database APIs for Java programmers. The JDBC API defines a number of classes in Java that represent database connections, SQL directives, result sets, database metadata, and so on. It allows Java programmers to send SQL instructions and process the results. Through the driver manager, the JDBC API can use different drivers to connect to different database systems.
Personal understanding is to use JDBC to load different database drivers to connect to the corresponding database, the following is a map of Baidu Encyclopedia to help understand.
Using JDBC to connect to a database mainly uses the following classes and interfaces:
Drivemanage Class driver management class for loading drivers
Connection Interface Link Database
Statement interface Execute SQL statement get query results
PreparedStatement interface to perform precompiled SQL statements (statement subinterfaces)
The ResultSet interface provides methods for processing the result set
The first thing to download is the appropriate JDBC for mysql:http://www.mysql.com/products/connector/
I am using eclipse, first import just download the extracted package (Mysql-connector-java-5.0.8-bin.jar),
1 Public classMysqldemo {2 3 Public Static FinalString dbdriver = "Org.gjt.mm.mysql.Driver";//Defining Drive Paths4 Public Static voidMain (string[] args) {5 //TODO auto-generated Method Stub6 Try{7Class.forName (Dbdriver);//Load Driver8}Catch(ClassNotFoundException e) {9 e.printstacktrace ();Ten } One A } - -}
If you can run the above code correctly then the driver is loaded normally. If the load is not successful, you can look at the guide package to see if the drive path is correct.
Have seen before also said is COM. The simplest way to do this is to use the decompression software to open your own and find the driver class.
Loading the driver next is to connect to the database via DriverManager's getconnection (string URL, string user, string password)
To get the Conneion instance, connect.
1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 Importjava.sql.SQLException;4 5 Public classMysqldemo {6 7 Public Static FinalString dbdriver = "Org.gjt.mm.mysql.Driver";//Drive Path8 Public Static FinalString Dburl = "Jdbc:mysql://localhost:3306/demo";//Database Path9 Public Static FinalString USER = "root";//User nameTen Public Static FinalString PASSWD = "changeme";//Password One Public Static voidMain (string[] args) { A //TODO auto-generated Method Stub -Connection con =NULL;//Database Connection Interface - Try{ theClass.forName (Dbdriver);//Load Driver -}Catch(ClassNotFoundException e) { - e.printstacktrace (); - } + Try{ -con = drivermanager.getconnection (DBURL,USER,PASSWD);//connecting to a database +}Catch(SQLException e) { A e.printstacktrace (); at } -System.out.println (con);//If the connection succeeds there will be output, otherwise throw an exception - Try{ -Con.close ();//Close the database -}Catch(SQLException e) { - e.printstacktrace (); in } - to + } - the}
Database normal connection Output database connection information similar to the following
[email protected]
I. Java connection to MySQL database via JDBC (connection)