A separate Java program connects to the MySQL database.

Source: Internet
Author: User

Today, I met a friend in the group and asked how to use a separate JavaProgramConnect to the MySQL database. Or any other database. I did not write this during my studies.CodeAre written in eclipse or myeclipse. Copy the jar package from the database to the Lib under the project, and then introduce it.
Today, I will write a simple Java file connecting to MySQL using Java. The result shows that the connection fails because the driver jar package cannot be found. Then, of course, I went to Google's boss and soon found the relevant instructions and documents. Then I passed the test myself. Therefore, write down your own simple feelings for future reference or for beginners who are new to Java to connect to the database.

 

Steps:

1. Download and install MySQL

2. Download the MySQL driver jar package. My version is:Mysql-connector-java-5.1.10-bin.jar

3. Copy the jar package file above to the JDK installation directory,D:/program files/Java/jdk1.6.0 _ 23/JRE/lib/EXT
Below, my installation directory is d disk, and other disks are similar.

4. Modify the environment variable classpath and add the following content:; D:/program files/Java/jdk1.6.0 _ 23/JRE/lib/EXT/mysql-connector-java-5.1.10-bin.jar,


That is, the name of the jar package directory and jar package file.
5. write code Test



The Code is as follows:

/** <Br/> * separate Java programs connect to the MySQL database <br/> * Author: javaalpha <br/> * Date: 20:48:28 <br/> * ide: e-Editor Version 9.16 <br/> */<br/> Import Java. SQL. *; <br/> public class test {<br/> Public static void main (string [] ARGs) {<br/> string driver = "com. mySQL. JDBC. driver "; <br/> string url =" JDBC: mysql: // FIG: 3306/test "; <br/> string user =" root "; <br/> string Password = "123456"; <br/> try {<br /> Class. forname (driver); <br/> connection conn = drivermanager. getconnection (URL, user, password); <br/> If (! Conn. isclosed () <br/> system. Out. println ("succeeded connecting to the database! "); <Br/> statement = Conn. createstatement (); <br/> string SQL = "select * From t_user order by id desc"; </P> <p> resultset rs = statement.exe cutequery (SQL ); <br/> string name = NULL; <br/> while (RS. next () <br/>{< br/> name = Rs. getstring ("sourse"); <br/> system. out. println (RS. getstring ("ID") + "/t" + Rs. getstring ("href") + "/t" + name + "/t" + Rs. getstring ("title") + "/t" + Rs. getstring ("time ") ); <Br/>}< br/> Rs. close (); <br/> Conn. close (); <br/>}catch (classnotfoundexception e) {<br/> system. out. println ("Sorry, can't find the driver! "); <Br/> E. printstacktrace (); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>}



Due to time constraints, errors are inevitable, and there is not much optimization and good code. Please see a lot of friends who have pointed out the errors.




Related Article

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.