By zieckey (zieckey@yahoo.com.cn)
All rights reserved! Assume that your Java development environment has been set up. I. Preparation: Download MYSQL: mysql-5.0.27-win32.zip
Download MySQL JDBC jar package: mysql-connector-java-5.0.3-bin.jar 2, installation and testing MySQL installation in Windows is very simple, note that in the selection of configuration is to MySQL configuration imported into the system environment variables,
This option is required for later convenience.
After the installation is complete, test the following:
1. Run MySQL Server
M: \ Documents and Settings \ Apple> mysqld-NT -- Help
Mysqld-nt ver 5.0.22-Community-NT for Win32 on ia32 (MySQL Community edition (G
PL ))
Copyright (c) 2000 MySQL AB, by Monty and others
This software comes with absolutely no warranty. This is free software,
And you are welcome to modify and redistribute it under the GPL licensestarts the MySQL database serverusage: mysqld-nt [Options] for more help options (several pages ), use mysqld -- verbose -- help here we can see that mysqld-nt.exe is a MySQL database server
Run the client to connect: if the system does not have this mysqld-nt command, it is likely that the MySQL configuration information is not imported into the system environment variable. At this time, you can import the bin directory under the MySQL installation directory to the system path environment variable, for example:
M: \ windows \ system32; m :\ windows; M: \ windows \ system32 \ WBEM; M: \ Program Files \ mysql \ MySQL Server 5.0 \ bin
Run mysqld-nt again. M: \ Documents ents and Settings \ Apple> mysqld-nt2. Connect to MySQL.
Format: mysql-H host address-u user name-P User Password
Example 1: connect to MySQL on the local machine.
First, open the DOS window, enter the directory mysqlbin, then type the command mysql-uroot-P, and press enter to prompt you to enter the password. If you have just installed MySQL, super User Root has no password, so press enter to enter mysql. The MySQL prompt is: mysql> M: \ Documents ents and Settings \ Apple> mysql-uroot-P.
Enter password:
Welcome to the MySQL monitor. commands end with; or \ G.
Your MySQL connection ID is 1 to server version: 5.0.22-Community-nt type 'help; 'or' \ H' for help. type '\ C' to clear the buffer. mysql> Example 2: connect to MySQL on the remote host. Assume that the IP address of the remote host is 110.110.110.110, the user name is root, and the password is abcd123. Enter the following command:
Mysql-h110.110.110.110-uroot-pabcd123
(Note: you do not need to add spaces to the U and root nodes.) 3. Exit MySQL command: exit (Press ENTER)
Mysql> exit
Byem: \ Documents ents and Settings \ Apple> 3. Use the MySQL database in Java
1. Quasi-database files
Create a learnjava database:
M: \ Documents and Settings \ Apple> mysql-uroot-P
Enter Password :******
Welcome to the MySQL monitor. commands end with; or \ G.
Your MySQL connection ID is 6 to server version: 5.0.22-Community-nttype 'help; 'or' \ H' for help. type '\ C' to clear the buffer. mysql> Create Database learnjava;
Query OK, 1 row affected (0.02 Sec) Create userinfo table:
Mysql> Use learnjava;
Database changed
Mysql> Create Table userinfo (
-> Username varchar (20) not null,
-> Userpwd varchar (20) not null
-> );
Query OK, 0 rows affected (0.06 Sec) mysql>
Insert a data entry:
Mysql> insert into userinfo values ('zieckey', '20140901 ');
Query OK, 1 row affected (0.03 Sec) mysql> 2. Import the JDBC jar package of MySQL into the system environment.
Here I put the mysql-connector-java-5.0.3-bin.jar under the installation directory of Java,
Then import it to the classpath environment variable, for example:
.; % Java_home % \ Lib \ tools. jar; % java_home % \ Lib \ mysql-connector-java-5.0.3-bin.jar; % java_home % \ Lib \ DT. jar; 3. Create a Java Program Queryjdbc. Java
/** * This program is used to test the connection between JDBC and MySQL. */ PackageTest; Import Java.SQL.*; Public Class Queryjdbc { Static { Try { // The driver and ODBC are different. Class . Forname ( "Org. gjt. Mm. MySQL. Driver" ) ; System . Out . Println ( "Success loading MySQL driver ...." ) ; } Catch ( Exception E ) { System . Out . Println ( "Error loading MySQL driver ....." ) ; E . Printstacktrace ( ) ; } }
/** * @ Param ARGs */ Public Static Void Main ( String AGRs [ ] ) { Try { // The Connection parameters are different from the access parameters. String URL = "JDBC: mysql: // localhost/learnjsp" ; // Establish a connection Connection Con = Drivermanager . Getconnection ( URL , "Root" , "011124" ) ; // Create a statement object for sending SQL commands Statement Stmt = Con . Createstatement ( ) ; // Return the query result Resultset RS = Stmt . Executequery ( "Select * From userinfo" ) ;
ResultsetmetadataMD=RS.Getmetadata ( ); Int Col = MD . Getcolumncount ( ) ; For ( Int I = 1 ; I < = Col ; I + + ) { System . Out . Println ( MD . Getcolumnname ( I ) + "\ T" ) ; } While ( RS . Next ( ) ) { String Strdata = "" ; For ( Int I = 1 ; I < = Col ; I + + ) { Strdata = Strdata + RS . Getstring ( I ) + "\ T" ; } System . Out . Println ( Strdata ) ; } // Disconnect the connection RS . Close ( ) ; Stmt . Close ( ) ; Con . Close ( ) ; } Catch ( Sqlexception E ) { E . Printstacktrace ( ) ; } }
}
|
E: \ javalesson \ mysqltest \ test> javac queryjdbc. Java-D.
E: \ javalesson \ mysqltest \ test> JAVA test. queryjdbc
Success loading MySQL driver ....
Username
Userpwd
Zieckey 123456, please note that if you create a project under eclipse,
The program is likely to run with an exception, prompting that org. gjt. Mm. MySQL. driver cannot be found,
You can choose add extenal jars from project> Properties> JAVA build path> libraries,
Then you can choose a third-party jar package: mysql-connector-java-5.0.3-bin.jar
In this way, the program can run normally. Note: Source http://zieckey.cublog.cn