如何在Tomcat+MySql环境下部署

如题所述

第1个回答  2016-08-04
下面用一个简单的例子来测试一下

  在tomcat/webapps/下建一个文件夹mysqlManager,创建目录mysqlManager/WEB-INF/classes

  mysqlManager/WEB-INF/web.xml

  在/mysqlManager/ 下新建index.jsp文件,内容如下

  /mysqlManager/index.jsp:

  <?xml version="1.0" encoding="gbk"
?>
<%@ page language="java" contentType="text/html; charset=gbk"

pageEncoding="gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@
page import="java.sql.*" %>
<%!
//这个函数用来获取一个和数据库的连接
Connection
getConnection() throws SQLException{
String
dbURL="jdbc:mysql://localhost/mysql";
String dbUser="root";
String
dbPassword="851120";
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException
e){
e.printStackTrace();
}
return
DriverManager.getConnection(dbURL,dbUser,dbPassword);
}

//判断一个字符串是否为空
boolean
isEmptyString(String str){
return
str==null||str.length()==0||str.trim().length()==0;
}
//下面的函数使显示的字符串为汉语
String
convert(String str){
try{
return new
String(str.getBytes("ISO-8859-1"),"gbk");
}catch(Exception e){
return
null;
}
}
%>

  <script
type="text/javascript">
<!--
function
checkValue(){
if(document.forms["main"].sql.value.length==0){
alert("请输入要执行的SQL语句");
document.forms["main"].sql.focus();
return
false;
}else{
return
true;
}
}
//-->
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta
http-equiv="Content-Type" content="text/html; charset=gbk"
/>
<title>mysql数据库管理系统</title>
</head>
<body>
<center>
<table
width="80%"
border="0">
<tr>请输入要查询的SQL语句</tr>
<tr>
<form
action="index.jsp" name=main method="post"
onsubmit="checkValue()">
<td>
<textarea name="sql"
cols=80 rows=15></textarea>
</td>
<td
valign="bottom">
<input type="submit"
value="提交"/>
</td>
</form>
</tr>
</table>
<hr
/>
<%//接受用户的输入显示查询结果 %>
<%
//获取用户输入的SQL语句
String
sql=request.getParameter("sql");
//out.print("qwe");
if(!isEmptyString(sql)){
%>
<p>您所输入的SQL语句是<%=sql
%></p>
<%
Connection con=null;
Statement
stmt=null;
ResultSet
rs=null;
//out.print("qwe");
try{
//out.print("qwe");
con=getConnection();
//out.print("qwe");
stmt=con.createStatement();
//stmt.execute(sql);
if(stmt.execute(sql)){
//执行结果是ResultSet
rs=stmt.getResultSet();
//获取rs自身的描述信息
ResultSetMetaData
rsmd=rs.getMetaData();
//获取rs的列数目
int
colNum=rsmd.getColumnCount();
%>
<p>您的查询结果为:</p>
<table
border="1">
<tr>
<%
for(int
i=1;i<=colNum;i++){
%>
<td><%=rsmd.getColumnName(i)
%></td>
<%
}
%>
</tr>
<%
while(rs.next()){
%>
<tr>
<%
for(int
j=1;j<=colNum;j++){
%>
<td><%=convert(rs.getString(j))
%></td>
<%
}
}
%>
</tr>
</table>
<%
}else{

int
colNum=stmt.getUpdateCount();
%>
<p>您更新了<%=colNum
%>列!</p>
<%
}
}catch(SQLException
e){
%>
<p>发生错误,请检查数据库的连接</p>
<%
}finally{
try{
if(rs!=null)
rs.close();
}catch(Exception
e){
}
try{
if(stmt!=null)
stmt.close();
}catch(Exception
e){
}
try{
if(con!=null)
con.close();
}catch(Exception
e){
}
}
}
%>
</center>
</body>
</html>
作者:
qiudawei115
相似回答