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