Connect to MYSQL using JDBC

Source: Internet
Author: User
Tags netbeans

It took me a morning to debug a program and finally succeeded. Although it was a few simple words, I had some gains in the process ~ I have a lot of feelings.

The question is to use JDBC to connect to MYSQL. It would have been okay to use ejbs directly without any connection. But I think if I just want to make a simple model, would I also use ejbs? So you need to know how to connect to the database.

I first downloaded the MYSQL driver from mysql. COM '~ Set include in CLASSPATH

The program is as follows ~~

Package com. myapp. struts;

Import com. mysql. jdbc. Driver;

Import java. SQL .*;

Import javax. servlet .*;

Import javax. servlet. http .*;

Import java. io .*;

/**

*

* @ Author zj395455

*/

Public class NewClass {

Public NewClass (){

}

Public static void main (String [] args ){

Try {

Class. forName ("com. mysql. jdbc. Driver ");

Connection conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/test", "root", "19850527 ");

System. out. print ("successful ");

}

Catch (Exception e)

{

System. out. print ("fail to connect the database ");

}

}

}

During the first writing, I wrote JAVA. SQL. *; JAVAX. SQL .*;

There have been many strange problems ~

Compile in NETBEANS ~ No com. mysql. jdbc package is displayed ~ I found no errors in all parts ~

Later, I asked for a master leave in the group ~ Experts tell me ~ NETBEANS does not support CLASSPATH

You don't need to read the content of CLASSPATH. 'fuck thing '~ Really big enough ~ If it weren't for the company's requirement to use this IDE, I would have changed it ~

Okay ~ Import the JAR package in the project ~ Later I found that I still didn't run it'

Operation ~~ Throwing an IDE

Handwritten code UE

The error is changed ~ Becomes an exception'

Java. lang. NoClassDefFoundError

Very strange problem ~ Obviously the file is in the current directory ~ The reminder class is not found ~~ Compilation is normal ~~

Later, I found out ~~

In CLASSPATH, set to.; other paths

The "." indicates the current directory ~~ This makes us suddenly realize that ~ The time has reached 11: 00 ~

You can still find the error ~

I am about to crash ~

Continue searching '~

Later I saw my own package ~~ Package com. myapp. struts;

I wonder if this package is not included or the package hides the path strength of this class.

~ Detailed exception prompt is set'

Sure enough ~ The error message "com/myqpp. struts" is not found.

It is OK to delete this statement.

After changing the password to a real password, SUCCESSFUL is output.

Results of one morning ~

Be careful in programming

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.