2013年8月25日星期日

Visit local tomcat mysql database

 

although previously often heard talk of tomcat, but today the first time to use tomcat.

 

1.Tomcat installation process:

 

should first be downloaded from the Apache website is for Windows. zip archive.

 

Here is the corresponding download link:

 

http://apache .etoak.com/tomcat/tomcat-7/v7.0.42/bin/apache-tomcat-7.0.42-windows-x64.zip

 

due LZ host is 64-bit, so I downloaded the 64-bit version of apache tomcat.

 

After downloading the archive, under the specified path can be extracted.

 

 

2. environment variable settings

 

want to run locally on the premise that tomcat already installed the JDK,

 

and already in the environment variable JAVA_HOME system variable is set will be set based on the success,

 

TOMCAT_HOME next step is to set up,

 

specific operation as shown below:

 

right-click My Computer, select Properties option.

 

for setting TOMAT_HOME is this,

 

 

 

 

 

 

 

environment variable is set, the next step is through XML documents relevant attributes for tomcat

 

set accordingly.

 

 

3. Through the Tomcat configuration XML file

 

Tomcat as APache open source projects, and other open source projects in the folder as well as set-top paths are very similar.

 

Tomcat configuration folder is conf, enter the conf folder can then be found to have

 

server.xml < / p>  

tomcat-users.xml

 

web.xml ,

 

and some other types of configuration files.

 

Here we only need to modify the tomcat-users.xml these two configuration files,

 

rest of the configuration file does not need modification.

 

port number if you want to modify, then, is the need to change the configuration file server.xml.

 

in accordance with its own situation on the port number changes, you can start a tomcat to see the situation.

 

into tomcat's bin subdirectory underneath this one,

 

will see startup.bat due to the current working system is windows,

 

so you can double-click startup.bat normally open a tomcat.

 

If the host system is Linux, then it, enter the command sh startup.sh

 

can start tomcat, but before that you need to change the permissions to 777.

 

 

started for the LZ is a tomcat console will appear after the flash of the situation,

 

a start thought it was some version of tomcat problem.

 

later replacement version, the problem still exists,

 

he intended to look into a flash of what information appears above the console prompt.

 

console displays can stay if you can click on it to see the message,

 

Right-click startup.bat, select the Edit button,

 

then go to the. bat editing panel above.

 

added above the last line:

 

PAUSE

 

so you see the console prompt.

 

 

based console message, that the original is not written in the JAVA_HOME environment variable.

 

reasons like this, because LZ using NetBeans development environment,

 

So, do not rewrite or add JAVA_HOME java program also can be run as usual.

 

 

/ * About W7 in the console

 

because the IDE environment itself will provide a console, the console should be on the current operating system is separate.

 

and the W7 OS is not what we generally call DOS DOS system,

 

it is only in simulation DOS, rather than be embedded in W7 in DOS system.

 

 

LZ cmd window when switching path, and will not receive, then use the power shell

 

instead receive command cmd window.

 

that is beginning -> All Programs -> Accessories windows power shell-> Windows PowerShell (x86)

 

choose this on it.

 

Next will pop up a DOS-like command line interface, enter the command entered, the console will all received.

 

In LZ view, the console should be running W7 corresponding simulated DOS operating system console written,

 

because it is analog, so the specific execute permissions based on the user's identity permissions are related, LZ family version of W7 is so limited privileges,

 

does not control the entire system to the current W7.

 

* /

 

 

configured JAVA_HOME in after that, you can re-run a tomcat,

 

fact, according to the LZ understand it, tomcat essentially using java language,

 

are many. class collection of files, of course, in order to facilitate these loads. class files have been packaged into. jar file.

 

so if you want it to run correctly, then,

 

that is running a start tomcat, tomcat session system variable to find JAVA_HOME

 

in the path of the current system, then the path (JAVA_HOME) to find the corresponding java editor,

 

is that we can identify and resolve .class / .jar documents related procedures.

 

then through the program right .class / .jar file to load, run, so down, then tomcat was successfully run.

 

 

4. turned tomcat

 

For server.xml file has not been modified in the case,

 

For local connection port number defaults to 8080,

 

so configured tomcat, you can be checked through a browser.

 

by double-clicking startup.bat;

 

can see the following display shows tomcat run successfully:

 

 

open Internet Explorer, the address bar in the corresponding input

 

http://localhost:8080 < / p>  

 

 

5.Tomcat connect with the database:

 

connected with the database must be carried out in the tomcat

 

create a user and then set the login password, and then assign the relevant permissions.

 

 

Use Notepad to open the way to open the tomcat-users.xml file.

 

In on the line before the following contents:

 
  
<!--
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
-->
</tomcat-users>
 
 

documentation according to the above comments before and after removing symbols: "" ""

 

 

then add a user, the user in creating and assigning passwords and permissions later,

 

can use the same username and password to log into the mysql,

 

mysql after login as that user to create databases, and database tables.

 

so tomcat can be visited.

 

uncomment after add on line you want to create the user name, password, and the need to allocate permission.

 

but add the user to follow the following format, you can save the file after entry.

 

 

 

then in mysql as the user name login:

 

mysql-uroot2-hlocalhost-p (enter)

 

Enter password: ***

 

 

LZ in this place, but did not proceed smoothly, and then changed a bit method:

 

as root into mysql in,

 

check the database information for all users:

 

select user, host from mysql.user;

 

(but this is after creating a user root2 screenshot)

 

 
  
create database school;
 
 

 

mysql then create and server.xml

 

the same user name and login name and password for the database where the host school

 

with tomcat server.xml configuration file in the same information for the mysql database creation,

 
  
create user "root2"; 

grant all on school to root2@localhost identified by "zhu";
 
 

 

this case, with regard to mysql and tomcat have the same user name and login password,

 

this case, tomcat can be very logical to root2 identity and access mysql landing in a school database,

 

also because in mysql create user root2 when assigned all the permissions on the database of the school,

 

so it can be passed through the tomcat jsp to create the database tables, delete, update, and so some of the operation.

 

 

So, how do you know tomcat has been able to successfully connect to the database of it?

 

can write a short. jsp code to test it,

 

first in the path below to create a Notepad file, and then adjust the properties to show hidden files file type name,

 

the "New Text File. txt" changed to "testjdbc.jsp",

 

Next, we write the testjdbc.jsp accordingly:

 

open file testjdbc.jsp, add the following to the file:

 
  
<%@ page contentType="text/html;charset=GB2312" %> 
<%@ page import="java.sql.*" %>
<HTML>
<BODY>
<%

String driver
="com.mysql.jdbc.Driver";
String url
="jdbc:mysql://localhost:3306/school"; // 连接到 school 数据库

String userid
="root2"; // 用户
String passwd="zhu"; // 密码


try{
Class.forName(driver);
}
catch(Exception e){
out.println(
" 无法载入 "+driver+" 驱动程序 !");
e.printStackTrace();
}


try {
Connection con
=DriverManager.getConnection(url,userid,passwd);
if(!con.isClosed())
out.println(
" 成功连接数据库 !");
con.close();
}
catch(SQLException SQLe){
out.println(
" 无法连接数据库 !");
}

%>
</BODY>
</HTML>
 
 

user name and password to connect to the database as well as can be modified according to the actual situation of,

 

 

since jsp file has been written, then how to load it into tomcat

 

them run the test it?

 

file should be placed in the testjdbc.jsp tomca of

 

\ webapps \ ROOT \ below, followed by the Internet Exploer in the address bar, enter:

 

http:/ / localhost: 8080/testjdbc.jsp

 

jsp scripts based on the preparation, if successful, then connect to mysql, there will be:

 

successfully connect to the database!

 

this message.

 

 

database if the connection fails:

 

unable to connect to the database!

 

this message.

 

 

always connected if it fails, you should check to see if you need to download a mysql-connector or something.

 

 

6. Summary

 

this time in the process of building tomcat learned a lot of knowledge,

 

personally feel that an XML document as an open source project configuration documentation really account for a large proportion,

 

If you have time, then the system will be dedicated to learn some of the XML syntax rules.

 

and hive in the database configuration file is written in XML.

 

 

In fact, the purpose of this test is to want to build a local POJ platform,

 

results build to know, download POJ platform tarball is just the OJ,

 

corresponding problem sets are not included in one of the last shows POJ although it is normal page,

 

but tomcat reported a lot of mistakes, they do not understand the current level LZ tomcat quoted the wrong ones,

 

then do not bother to apache's official online face to check some relevant error message, so .... helpless call it a day.

 

 

LZ feel it ......

 

POJ platform structures is not difficult, step is relatively complex,

 

and complicated reason is because many of POJ platform system environment variables are relatively rigid,

 

without any machine automatically adapted according to the local optimization measures.

 

such as tomcat can only be installed in D: \ disc below,

 

mysql can only be installed in the D: \ disk below. And the default port 8080, the port must be changed to 80, and so on.

 

 

spent a whole day, but has been exposed to the tomcat always wanted to understand, and still very happy.

 

 

still concentrate on AC next time, huh, huh.

 

没有评论:

发表评论