Connect to MySQL using Java

Source: Internet
Author: User

This article uses MySQL as an example to describe how to connect Java to a database.

Of course, you must first install JDK (usually JDK 1.5.x ). Then install MySQL, which is relatively simple and the specific process will not be mentioned. After these two environments are configured, download the JDBC driver mysql-connector-java-5.0.5.zip (the latest version ). Decompress the package to any directory. I decompress the package to drive D and add the mysql-connector-java-5.0.5-bin.jar under its directory to classpath as follows: my computer-> properties-> advanced-> environment variables, edit classpath in system variables, add D: \ mysql-connector-java-5.0.5 \ mysql-connector-java-5.0.5-bin.jar to the end, add ";" before adding this string to distinguish it from the previous classpath. Then confirm.

After the environment is configured, it is very simple. Now, configure MySQL with the username "root" and password "root ". Create a database on the command line or using an SQL front-end software.

I used sqlyog's front-end software to create a database.

Create a database first:

Create Database scutcs;

Create a table:

Create Table student

(

Sno char (7) Not null,

Sname varchar (8) Not null,

Sex char (2) not null,

Bdate date not null,

Height Dec (000.00) default,

Primary Key (SNO)

);

Then insert data. You can use the SQL statement insert into <Table Name> values (value1, value2 ,...);

You can also use sqlyog to perform operations.

 

OK.

Next, we will compile the. Java file to demonstrate how to access the MySQL database.

1 import Java. SQL. *; 2 3 Public class jdbctest {4 5 public static void main (string [] ARGs) {6 7 // driver name 8 string driver = "com. mySQL. JDBC. driver "; 9 10 // The URL points to the name of the database to be accessed scutcs11 string url =" JDBC: mysql: // 127.0.0.1: 3306/scutcs "; 12 13 // MySQL configuration username 14 string user = "root"; 15 16 // MySQL configuration password 17 string Password = "root "; 18 19 try {20 // load driver 21 class. forname (driver); 22 23 // continuous database 24 connection C ONN = drivermanager. getconnection (URL, user, password); 25 26 if (! Conn. isclosed () 27 system. Out. println ("succeeded connecting to the database! "); 28 29 // statement is used to execute SQL statement 30 Statement statement = Conn. createstatement (); 31 32 // SQL statement to be executed 33 string SQL = "select * from student"; 34 35 // result set 36 resultset rs = statement.exe cutequery (SQL ); 37 38 system. out. println ("-----------------"); 39 system. out. println ("the execution result is as follows:"); 40 system. out. println ("-----------------"); 41 system. out. println ("student ID" + "\ t" + "name"); 42 system. out. println ("---------------- -"); 43 44 string name = NULL; 45 46 While (RS. next () {47 48 // select the sname column for Data 49 name = Rs. getstring ("sname"); 50 51 // first use the ISO-8859-1 character set to decode the name into a byte sequence and store the results in a new byte array. 52 // then use the gb2312 character set to decode the specified byte array 53 name = new string (name. getbytes ("ISO-8859-1"), "gb2312"); 54 55 // output result 56 system. out. println (RS. getstring ("Sno") + "\ t" + name); 57} 58 59 Rs. close (); 60 Conn. close (); 61 62} catch (classnotfoundexception e) {63 64 65 system. out. println ("Sorry, can't find the driver! "); 66 E. printstacktrace (); 67 68 69} catch (sqlexception e) {70 71 72 E. printstacktrace (); 73 74 75} catch (exception e) {76 77 78 E. printstacktrace (); 79 80 81} 82} 83}

 

Next let's run it to see the effect:

D: \ testjdbc> javac jdbctest. Java

D: \ testjdbc> JAVA jdbctest
Succeeded connecting to the database!
-----------------------
The execution result is as follows:
-----------------------
Student ID name
-----------------------
0104421 weeks
0208123 Wang Yiping
0209120 Wang DaLi
0309119 Li Wei
0309203 Ouyang Merrill Lynch

Haha, success

 

[Transfer] http://www.cnblogs.com/soplayer/archive/2007/06/26/796565.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.