Eclipse + Tomcat + Mysql build J2EE development environment (original) Tomcat
Reproduced from: http://blog.csdn.net/wdwgr/article/details/1586155
The threshold for learning j2ee is the configuration of the development environment. I feel like I can't start without the development environment. I am so worried for a long time. Finally, I chose eclipse + tomcat + mysql, there are also many articles on development environment configuration on the Internet, but they are not detailed enough. I tried to summarize it myself. After two days of configuration, I finally configured the environment, I think my efforts over the past two days have finally paid off. now I will share with you the fruits of my work!
Jdk, eclipse, tomcat, and mysql need to be downloaded. they are all open-source.
1. install jdk by following the steps below. The key is to right-click my computer after installing the environment variable --> properties --> advanced --> environment variable --> system variable: add the environment variable: (If your jdk is installed in the c: root directory ):
JAVA_HOME: C:/jdk;
Classpath =.; % JAVA_HOME %/lib/dt. jar; % JAVA_HOME %/lib/tools. jar; (.; cannot be less, because it represents the current path)
Path = % JAVA_HOME %/bin
Episode: (What is CLASSPATH? What is its role?
It is an environment variable of the javac compiler.
Its role is related to the import and package keywords.
When you write improt java. util. * When the compiler faces the import keyword, it will know that you want to introduce java. util is a class in the package. but how does the compiler know where you put the package? So you must first tell the compiler where the package is located. how can you tell it? Set CLASSPATH: if the package of java. util is in the c:/jdk/directory, you must set the path of c:/jdk/to CLASSPATH! When the compiler faces the import java. util. * In this statement, it first looks for the directory specified by CLASSPATH, checks whether the subdirectory java/util exists, and then finds the compiled file (. class file ). If not found, an error is returned!
CLASSPATH is a bit like the INCLUDE path settings in the c/c ++ compiler, isn't it? When the c/c ++ compiler encounters include How does such a statement work? Oh, the truth is almost the same! Search INCLUDE path and view file!
When you develop a package, you want to use the classes in the package. Naturally, you have to set the Directory of the package to CLASSPATH!
CLASSPATH settings are tricky for beginners of JAVA. So Sun makes JDK of JAVA2 smarter. You will find that after installation, even if you have not set CLASSPATH, you can still compile and execute basic JAVA programs .)
Then you can write a program to test whether the installation is successful:
Public class Test {
Public static void main (String args []) {
System. out. println ("hello world .");
}
}
Save the above program as Test. java, open the command prompt window, cd to the folder where your program is located, and enter the following command:
Javac Test. java press enter // compile the program
Java Test Press enter // interpreter
If the output is hello world, the installation is successful. if the compilation fails, check the environment variable settings carefully.
2. install tomcat, which is easy not to be described. after installation, right-click my computer and choose "properties"> "advanced"> "environment variable"> "system variable: add the environment variable: (If your tomcat is installed in the c:/root directory)
CATALINA_HOME = C:/tomcat; // catalina is the name of the servlet container implemented by Apache used by the Tomcat server.
CATALINA_BASE = C:/tomcat;
TOMCAT_HOME = C:/tomcat;
Modify the classpath in the environment variable, and append the servlet. jar under the common/lib directory of tomcat installation to the classpath. modify the classpath as follows:
Classpath =.; % JAVA_HOME %/lib/dt. jar;
% JAVA_HOME %/lib/tools. jar;
% CATALINE_HOME %/common/lib/servlet. jar;
Start tomcat and access http: // Localhost: 8080 in IE. if you see the welcome page of tomcat, congratulations on your successful installation;
3. install mysql. I use the free installation version (I prefer green software). since it is free of installation, of course, some settings should be pulled as follows:
(1) extract mysql-noinstall-5.0.27-win32.zip to D:/mysql5
(2) create the file my. ini in d:/mysql as follows:
#===================================================== =
[WinMySQLAdmin]
# Use a forward slash/to represent the path
Server = D:/mysql5/bin/mysqld-nt.exe
# Chinese record supported
Default-character-set = gb2312
[Client]
# Chinese record supported
Default-character-set = gb2312
[Mysqld]
Basedir = D:/mysql5
Datadir = D:/mysql5/data
Character_set_server = utf8
# Chinese record supported
Default-character-set = gb2312
# Transaction engine
Default-storage-engine = INNODB
#===================================================== =
(3) enter d:/mysql/bin to install the system service.
Install the service command:
D: mysql/bin> mysqld-nt.exe -- install (be sure to be -- install instead of-install)
Enter the command prompt window cd to: d:/mysql/bin
Enter the following command:
Mysql-nt-install
Mysql-uroot-p
Password: Press enter again, indicating that the password is empty. now the mysql prompt is displayed, and our mysql has been installed successfully;
4. install eclipse, decompress it directly (I put it under D:/Program Files/eclipse), but don't be too happy, this is just a single eclipse, plug-ins must be installed to provide powerful functions. The plug-ins we need here include tomcatplugin (because we need to work with tomcat) and mysqlplugin (used to connect to the database)
There are many other things we will not introduce here.
Tomcat software package: com. sysdeo. eclipse. tomcat
Mysql package: net. sf. jfacedbc_2.2.1
How to install these plug-ins? Simple: decompress them directly to the plug-ins folder under the eclipse installation directory. (remember to copy the org under the configuration Directory of the eclipse installation directory. eclipse. delete the update folder, or eclipse cannot load the plug-in you added)
After the work is done
Open eclipse --- Window ----> preferences, click the tomcat option in the left directory, and set it on the right. First, select the tomcat version that you have installed and the version that you have installed, select the Tomcat installation directory for tomcat home, and then expand tomcat, JVM Settings -- jre select jdk (select jdk instead of jre because jdk has the java Runtime Environment and the classes required by the java program, jre only has the runtime environment.
), Click apply, restart eclipse, and check that there are three more kittens in the function Bar. are you very happy? let's test tomcat:
Program writing
(1) create a Tomcat project
Right-click the blank area in the "package Resource Manager" window, choose "create"> "Tomcat Project", enter the Project name in the Project name, enter tomcat_test here, and click "finish.
(2) create a JSP page
In package explorer, right-click tomcat_test, create a file, and enter HelloWorld. jsp in the file name (the extension cannot be missing). in the text editing window, enter the following code:
Hello World
Save the settings. Start tomcat, as long as you enter http: // * in the browser window ://*. *. *. *: 8080/tomcat_test/HelloWorld. the jsp browser displays "Hello World", indicating that your jsp page is successfully deployed and published (*. *. *. * indicates the IP address of the host where the tomcat server is installed ).
5. the rest is to connect to the database, it is also very simple to download the mysql-connector-java-3.2.0-alpha-bin.jar directly put it in the C: tomcat/commen/lib directory, and then add it to the environment variable, add C:/tomcat/commen/lib/mysql-connector-java-3.2.0-alpha-bin.jar in the environment variable;
Our configuration work is almost complete now. next we will test it: first, make sure that your database has a test table, create a project Test and add a project as on the first jsp page we created. jsp file named test. jsp, add the following code:
<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "java. SQL. *" %>
<%
Try {
Class. forName ("com. mysql. jdbc. Driver"). newInstance (); // load the database
}
Catch (ClassNotFoundException error ){
System. err. println ("Unable to load the JDBC/MYSQL driver." + error. getMessage ());
System. exit (1 );
}
String url = "jdbc: mysql: // localhost/test? User = root & password = ";
// Assume that test is your database.
Connection conn = null;
Try {
Conn = DriverManager. getConnection (url );
}
Catch (SQLException error)
{
System. err. println ("Cannot connect to the database." + error );
System. exit (2 );
}
Statement stmt = conn. createStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
String SQL = "select * from pet"; // generally, the query statement is assigned to a String object.
// Read data from the test table
ResultSet rs1_stmt.exe cuteQuery (SQL );//
While (rs. next () {%>
The content of your first field is: <% = rs. getString (1) %>
Your second field content is: <% = rs. getString (2) %>
<% }%>
<% Out. print ("database operation successful, congratulations"); %>
<% Rs. close ();
Stmt. close ();
Conn. close ();
%>
Then save, start tomcat, open IE, input http: // 127.0.0.1: 8080/Test/test. jsp, and output the data in your database?
Congratulations, you can enjoy your hard work (if the plug-in involved in the article can not find, you can send me an email, wdwgr@163.com