2、将你的jsp项目放在tomcat的webapps目录里面,
3、再启动tomcat,启动好了之后你可以放一个html静态页面在jsp项目里面,如果html能访问,你的jsp项目就部署成功了,再访问你的jsp页面;
<%@ page contentType="text/htmlcharset=gb2312" import="java.sql.*, javax.sql.*, javax.naming.*"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">
<title>jdbc.jsp witshare</title>
</head>
<body>
<h2>将信息保存到数据库中</h2>
<%
Connection con = null
Statement stmt = null
Statement stmt1 = null
ResultSet rs = null
request.setCharacterEncoding("gb2312")
String employeeId = null
String lastName = request.getParameter("lastName")
String firstName = request.getParameter("firstName")
String birth = "1979/10/30"
String sex = "M"
String email = "aaa@bbb.com"
String newLastName = ""
String newFirstName = ""
%>
接收的信息<br>
姓:<%=lastName %>
名:<%=firstName %><br><br>
<%
try {
String driver = "com.mysql.jdbc.Driver"
String url = "jdbc:mysql://localhost:3306/sample_db?user=root&password=root&useUnicode=true&characterEncoding=GB2312"
Class.forName(driver)
con = DriverManager.getConnection(url)
stmt = con.createStatement()
String upd = "INSERT INTO employee(employee_id, last_name, first_name, birth, sex, email)" +
"VALUES ("+employeeId+", '"+lastName+"', '"+firstName+"', '"+birth+"', '"+sex+"', '"+email+"')"
stmt.executeUpdate(upd)
stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE)
String query = "SELECT * FROM employee"
rs = stmt1.executeQuery(query)
rs.last( )
newLastName = rs.getString("last_name")
newFirstName = rs.getString("first_name")
} catch(SQLException sqle) {
out.println("sqle=" + sqle)
} finally {
try {
if(rs != null) {
rs.close()
}
} catch(SQLException sqle) {
out.println("sqle=" + sqle)
}
try {
if(stmt != null) {
stmt.close()
}
} catch(SQLException sqle) {
out.println("sqle=" + sqle)
}
try {
if(stmt1 != null) {
stmt1.close()
}
} catch(SQLException sqle) {
out.println("sqle=" + sqle)
}
try {
if(con != null) {
con.close()
}
} catch(SQLException sqle) {
out.println("sqle=" + sqle)
}
}
%>
从employee表取出的新增的姓名<br>
姓名:<%= newLastName+newFirstName %><br>
</body>
</html>
---------------------------------------------------------
锐志陈鹏 专注Java/.Net培训
锐志技术社区:http://www.witshare.org/bbs/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)