JSP + MySQL website environment configuration in Windows

Source: Internet
Author: User
This document summarizes the installation of JSP websites by referring to the information on the Internet.

MySQL is used in the database (if any problem occurs during the configuration process, restart Tomcat for a try) I. download the software
1. j2se
Http://java.sun.com/j2se/1.4.2/download.html2.atatserver
Http://tomcat.apache.org/download-41.cgi3.mysqlserver
Http://dev.mysql.com/downloads/mysql/5.0.html
Select Latin1 as the character set during installation (same as the character set used by the database)
After the database is installed, run the following command to check the character set used by the database:
Help4.jdbc driver
Http://dev.mysql.com/downloads/connector/j/5.0.html 2. Install the above software in sequence
Assume that the paths after installation are as follows:
1. j2se
C:/j2sdk1.4.22. Tomcat
C:/program files/Apache Software Foundation/tomcat 4.13.mysql
C:/program files/MySQL Server 5.0 III. Configuration
1. Set Environment Variables
Right-click 'my pc' and choose Properties> advanced> environment variable to add the following variable: Path = % PATH %; C:/j2sdk1.4.2/bin; C: /program files/MySQL Server 5.0/bin
Java_home = C:/j2sdk1.4.2
Classpath = C:/j2sdk1.4.2/lib/tools. jar; mysql-connector-java-5.0.5-bin.jar; mysql-connector-java-5.0.5-bin-g.jarTOMCAT_HOME = C:/program files/Apache Software Foundation/tomcat 4.12.jsp connection MySQL settings
Set
Mysql-connector-java-5.0.5-bin.jar
Mysql-connector-java-5.0.5-bin-g.jar
Copy to C:/j2sdk1.4.2/lib
Mysql-connector-java-5.0.5-bin.jar
Copy
C:/program files/Apache Software Foundation/Tomcat 4.1/common/lib
C:/program files/Apache Software Foundation/Tomcat 4.1/shared/lib
(Do not copy mysql-connector-java-5.0.5-bin-g.jar) 4. jsp connection MySQL Test
1. Create Database Xia
Open the command line window and enter:
Mysql-H localhost-u root-P
Create Database Xia;
Use Xia;
Create Table member (ID int (8) primary key, name varchar (10 ));
Insert into Member values (1, 'yang ');
Insert into member (name, ID) values ('xia ', 2); (Security: Set permissions for the database (user and password)
Command: grant all privileges on shujuku. * To test @ localhost identified by "123456 ";
After you execute this command, you can only operate the shujuku database as long as you log on with the username: Test and password: 123456,
This avoids the use of root, which is of great help to database security .) 2. save the following file as index. JSP to C:/program files/Apache Software Foundation/Tomcat 4.1/webapps/root
---------------- Begin ----------------------
<% @ Page contenttype = "text/html; charset = gb2312" %> <% @ page Language = "Java" %> <% @ page import = "com. mySQL. JDBC. driver "%> <% @ page import =" Java. SQL. * "%> <% // driver name string drivername =" com. mySQL. JDBC. driver "; // database username string username =" root "; // password string userpasswd =" "; // database name string dbname =" xia "; // table name string tablename = "member"; // concatenate string url = "JDBC: mysql: // localhost/" + dbname + "? User = "+ username +" & Password = "+ userpasswd; Class. forname ("com. mySQL. JDBC. driver "). newinstance (); connection = drivermanager. getconnection (URL); Statement statement = connection. createstatement (); string SQL = "select * from" + tablename; resultset rs = statement.exe cutequery (SQL); // obtain the data result set resultsetmetadata rmeta = Rs. getmetadata (); // determines the number of columns in the dataset, and the number of fields int numcolumns = rmeta. getcolumncount (); // output each data value out. print ("ID"); out. print ("|"); out. print ("name"); out. print ("<br>"); While (RS. next () {out. print (RS. getstring (1) + ""); out. print ("|"); out. print (RS. getstring (2); out. print ("<br>");} Out. print ("<br>"); out. print ("database operation successful, congratulations"); RS. close (); statement. close (); connection. close (); %> ------------------ end ------------------------
Enter http: // localhost: 8080 Test 5. Install the website program 1). copy the file.
1. Copy C:/program files/MySQL Server 5.0/data/dataname
2.
C:/program files/Apache Software Foundation/Tomcat 4.1/webapps/root/mywebroot
C:/program files/Apache Software Foundation/Tomcat 4.1/webapps/root/Web-INF
Directory copied
3. Create
C:/program files/Apache Software Foundation/Tomcat 4.1/webapps/root/Web-INF/classes
Copy the package used
C:/program files/Apache Software Foundation/Tomcat 4.1/webapps/root/Web-INF/classes
Directory 2). Test the installation and configuration of JavaBean.
Create your own bean:
1. File Name testbean. Java:
-------- Begin --------- package test;
Public class testbean {
Private string name = NULL;
Public testbean (string strname_p ){
This. Name = strname_p;
}
Public void setname (string strname_p ){
This. Name = strname_p;
}
Public String getname (){
Return this. Name;
}
} --------- End ------------- 2. Compile
Place testbean. Java in C:/test and compile it with the following command:
C:/test> javac testbean. Java
Then a compiled bean file testbean. class will be generated in C:/test.
3. Cut the testbean. Class file to C:/program files/Apache Software Foundation/Tomcat 4.1/webapps/root/WEB-INF/classes/test
4. Create a New testbean. jsp file with the following content:
<% @ Page import = "test. testbean" %>
<HTML> <body> <center>
<%
Testbean = new testbean ("this is a test Java Bean .");
%>
Java Bean name is: <% = testbean. getname () %>
</Center> </body> 5. Restart tomcat, start the browser, and enter http: // localhost: 8080/testbean. jsp.
If you see the output Java Bean name is: this is a test Java Bean. It indicates that the bean has been compiled successfully. Vi. Problem Solving
1. Page garbled characters
Add
<% @ Page contenttype = "text/html; charset = gb2312" %> 2. garbled MySQL results
Solution 1: When connecting to MySQL (whether reading or retrieving data from MySQL), specify the encoding method to gb2312. The specific code is as follows // load the mysql-JDBC driver
Class. forname ("com. MySQL. JDBC. Driver"). newinstance ();
// Connect to the database
Connection sqlcon = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306/test? User = root & Password = 1 & useunicode = true & characterencoding = gb2312 "); solution 2: if method 1 does not work, perform forced encoding conversion on the read String Based on method 1. Sample Code: string name = RST. getstring ("name ");
Name = new string (name. getbytes ("ISO-8859-1"), "gb2312"); note: the code can also be: string name = new string (RST. getstring ("name "). getbytes ("ISO-8859-1"), "gb2312"); where RST is the returned resultset, The ISO-8859-1 is the default MySQL encoding method, the purpose of the code is to convert the ISO-8859-1 encoding to gb2312 encoding mode, this forced conversion, can solve a part of the problem, if combined with method 1, it should be able to solve the Chinese garbled problem.

 

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.