MySQL loads the JDBC driver and mysql loads the jdbc driver.
-
First, install the MySQL database. MySQL5.5 is installed. The installation steps are not described here. Please note that if the installation process stops at the start service and cannot continue, please refer to my blog post "when installing MySQL5.5, the start service does not respond, solution", it should help you solve the problem.
Next, go to MySQL Official Website. This is an image file. Double-click it to automatically install the file and generate a MySQL Connector J folder in the C: \ Program Files \ MySQL folder. Go to the folder and find a file named mysql-connector-java-5.1.35-bin.jar.
My java program is installed in the D: \ Java \ jdk1.7.0 _ 15 folder, find the lib folder under the folder, and copy the mysql-connector-java-5.1.35-bin.jar to lib. When the java Program is installed, a lib folder is generated in C: \ Program Files \ Java \ jre by default. in this folder, locate ext and double-click to enter the folder, at the same time, copy a copy of The mysql-connector-java-5.1.35-bin.jar into it.
-
-
Remember to configure CLASSPATH:
-
-
My CLASSPATH configuration :.; % JAVA_HOME % \ lib; % JAVA_HOME % \ lib \ dt. jar; % JAVA_HOME % \ lib \ tools. jar; % JAVA_HOME % \ lib \ mysql-connector-java-5.1.36-bin.jar;
-
Use normally in dos environment;
-
In Eclipse, right-click the project, right-click Build Path, and select Configure Build Path. An Attribute diagram is displayed. Select Libraries under Java Build Path, click Add External JARs, browse to the JDBC MySQL driver jar package, and click OK to import it to the project.
After completing the preceding steps, you can use java to operate the MySQL database.
-
- // There are only four steps: 1. Load the driver; 2. Connect to the database; 3. Execute the command; 4. Disable the database;
- Import java. SQL .*;
- Class MysqlConnection
- {
- /* If you change the database, replace these statements directly. The main function does not need to be moved, mainly because of the driver and the connection method (DBURL )*/
- Private static String DBDRIVER = "org. gjt. mm. mysql. Driver"; // This corresponds to the driver. class file downloaded from jdbc-mysql. You
- // You can decompress the package and find out that the driver is the ghost ..
- Private static String DBURL = "jdbc: mysql: // localhost: 3306/study ";/*
- The jdbc: mysql: // localhost: 3306: test statement is parsed as follows:
- Jdbc: mysql: // indicates the JDBC connection method;
- Localhost: Your local address;
- 3306 sqldatabase port number;
- Study is the address of the database you want to connect.
- You can try not to use this 'Study ', or randomly connect to a nonexistent database,
- Then you can run the following statement to connect to the database ()
- */
- Private static String DBUSER = "scott ";
- Private static String DBPASSWORD = "tiger ";
- Public static void main (String [] args) throws Exception
- {
- Class. forName (DBDRIVER); // 1. Load the driver
- Connection conn = DriverManager. getConnection (DBURL, DBUSER, DBPASSWORD); // 2. Obtain the link
- Statement statement = conn. createStatement (); // 3. Execute the command
- // Statement.exe cuteUpdate ("use study"); // (a) if the above database does not exist, use this function to connect to the database.
- ResultSet result = statement.exe cuteQuery ("SELECT * FROM emp"); // result collection, Iteration
- While (result. next ()){
- Printf (result. getObject (1) + "");
- Printf (result. getObject (2) + "");
- Printf (result. getObject (3) + "");
- Printf (result. getObject (4) + "\ n ");
- }
- Conn. close ();
- }
- Public static void printf (Object obj ){
- System. out. print (obj );
- }
- Public static void printfln (Object obj ){
- System. out. println (obj );
- }
- }
-
-
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.