JSP作业2017.4.5

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP ‘001.jsp‘ starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<form name="form1" method="post" action="002.jsp">
<p>&nbsp;</p>
<p>name:
<label for="textfiled"></label>
<input type="text" name="myname" id="textfiled1">
</p>
<p>class:
<label for="textfiled"></label>
<input type="text" name="myclass" id="textfiled2">
</p>
<input type="submit" name="button" id="button" value="提交">

</form>
</body>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<jsp:useBean id="myjdbc01" scope="page" class="com.lty.www.DBConn" />
<%
request.setCharacterEncoding("utf-8");
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String myname=request.getParameter("myname");
String myclass=request.getParameter("myclass");

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP ‘002.jsp‘ starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<%=myname%><br>
<%=myclass%><br>
<%

String insertsql="insert into message(name,class) values(‘"+myname+"‘,‘"+myclass+"‘)";
myjdbc01.doInsert(insertsql);
myjdbc01.close();

%>

</body>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="com.mysql.jdbc.*" %>

<jsp:useBean id="myjdbc01" scope="page" class="com.lty.www.DBConn"/>
<%

request.setCharacterEncoding("utf-8");
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP ‘001.jsp‘ starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>

<%
String sql="select * from message";
ResultSet rs = myjdbc01.doSelect(sql);
out.println("<table width=‘200‘ border=‘1‘>");
out.println("<tr>");
out.println("<th scope=‘col‘>学号</th>");
out.println("<th scope=‘col‘>班级</th>");
while (rs.next()) {
out.println("<tr>");
out.println("<td>");
out.println(rs.getString(1));
out.println("</td>");
out.println("<td>");
out.println(rs.getString(2));
out.println("</td>");}
out.println("</table>");

%>

</body>
</html>

package com.lty.www;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class DBConn {

private Connection conn=null;
private PreparedStatement stmt=null;
private ResultSet rs=null;

public DBConn(){

try {
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/student";
String user="root";
String password="1234";
Class.forName(driver);
conn = (Connection) DriverManager.getConnection(url,user,password);
System.out.println("-------连接成功------");
} catch(ClassNotFoundException classnotfoundexception) {
classnotfoundexception.printStackTrace();
System.err.println("db: " + classnotfoundexception.getMessage());
} catch(SQLException sqlexception) {
System.err.println("db.getconn(): " + sqlexception.getMessage());
}

}

public DBConn(String driver,String url,String user,String password){

try {
//driver="com.mysql.jdbc.Driver";
//url="jdbc:mysql://localhost:3306/school";
//user="root";
//password="711109";
Class.forName(driver);
conn = (Connection) DriverManager.getConnection(url,user,password);
System.out.println("-------连接成功------");
} catch(ClassNotFoundException classnotfoundexception) {
classnotfoundexception.printStackTrace();
System.err.println("db: " + classnotfoundexception.getMessage());
} catch(SQLException sqlexception) {
System.err.println("db.getconn(): " + sqlexception.getMessage());
}

}

public void doInsert(String sql) {
try {
stmt = (PreparedStatement)conn.prepareStatement(sql);
int i = stmt.executeUpdate(sql);
} catch(SQLException sqlexception) {
System.err.println("db.executeInset:" + sqlexception.getMessage());
}finally{

}
}

public void doDelete(String sql) {
try {
stmt = (PreparedStatement)conn.prepareStatement(sql);
int i = stmt.executeUpdate(sql);
} catch(SQLException sqlexception) {
System.err.println("db.executeDelete:" + sqlexception.getMessage());
}
}

public void doUpdate(String sql) {
try {
stmt = (PreparedStatement)conn.prepareStatement(sql);
int i = stmt.executeUpdate(sql);
} catch(SQLException sqlexception) {
System.err.println("db.executeUpdate:" + sqlexception.getMessage());
}
}

public ResultSet doSelect(String sql) {
try {
stmt = (PreparedStatement)conn.prepareStatement(sql);
rs = stmt.executeQuery(sql);
System.out.println("取得结果集");
} catch(SQLException sqlexception) {
System.err.println("db.executeQuery: " + sqlexception.getMessage());
}
return rs;
}

public void close(ResultSet rs) throws SQLException, Exception {

if (rs != null) {
rs.close();
rs = null;
}

if (stmt != null) {
stmt.close();
stmt = null;
}

if (conn != null) {
conn.close();
conn = null;
}
}

public void close() throws SQLException, Exception {
if (stmt != null) {
stmt.close();
stmt = null;
}

if (conn != null) {
conn.close();
conn = null;
}
}

public static void main(String []args) throws Exception {
DBConn db=new DBConn();
ResultSet rs=db.doSelect("select * from dbuser where userNAME=‘chenjirong‘");
try {
while(rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

rs.close();
db.close();
}
}

时间: 2024-10-23 03:10:09

JSP作业2017.4.5的相关文章

JSP作业

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServ

作业 2017/3/20

一.元素分类 有如下值集合[11,22,33,44,55,66,77,88,99,90 ...],将所有大于66的值保存至字典的第一个key中,将小于66的值保存至第二个key的值中. 即:{'k1':大于66的所有值,'k2'小于66的所有值} 二.查找 查找列表中元素,移动空格,并查找以a或A开投并且以c结尾的所有元素. li = ["alec"," aric","Alex","Tony","rain"

JSP作业3-金字塔

输出三角形 <!-- 金字塔--> <% String str1=""; for(int i=0;i<10;i++) { for(int j=0;j<10-i;j++) { str1+="&nbsp&nbsp"; } for(int k=0;k<2*i-1;k++) { str1+="*"; } str1+="<br>"; } %> <%=str1 %

JSP作业2-输出当前时间

当前时间 <!-- 当前时间--> <br> <% Date dd=new Date();%> <% String str = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(dd);%> <%=str%> 运行结果:

JSP作业1--5!

输出5的阶乘 <body> <!-- 5的阶乘--> <% int s=1; for(int j=1;j<6;j++) { %> <%s*=j; %> <% }%> <br> 5的阶乘是: <%=s %> 结果:

Servlet+JSP+JavaBean开发模式(MVC)介绍

好伤心...写登陆注册之前看见一篇很好的博文,没有收藏,然后找不到了. 前几天在知乎上看见一个问题,什么时候感觉最无力. 前两天一直想回答:尝试过google到的所有solve case,结果bug依然在. 今天想回答:明明遇见过,就是找不到那篇文的时候.哭! ================================================ 学习mvc模式(其实是JSP作业),当然这个不只是Servlet+JSP+JavaBean一种case,它是一种思想,实现web系统的职能分

软件工程第三次作业 - 每周例行汇报

本周PSP, 类型.任务.开始时间.结束时间.中断时间,delta时间.要求开始和结束时间包括日期.从作业要求公布之时起,至截止时刻,其间的工作有效,也本周四晚和周五全天.包含本周六.本周日. 本周进度条: 代码行,博文字数,用到的知识点 累积进度图: 到本周为止的代码累积折线图,到本周为止的博文字数累积折线图 本周PSP饼状图,按任务类别分类,给出时间所占比例 本周PSP 类型 任务 开始时间 结束时间 耽误时间(分钟) 耽误时间说明 △(分钟) 学习 学习英语 2017.9.21  18:0

第三周课程进度

表1 时间记录日志 学生     李婷婷                              日期   2017/12/11 教师     王建民                              课程  软件工程概论 日期 开始时间 结束时间 中断时间 净时间 活动 备注 2017/12/11(周一) 8:00 16:10 -- -- 上课 16:20 18:00 -- -- 补写<数据结构>作业,时间长了,忘性快. 19:00 21:30 -- -- 选修结课,练健美操. 2

Compiler_词法分析_表驱动法_分析文件

本文出自:http://blog.csdn.net/svitter 一开始做这个实验真是觉得各种简单- -就是一张状态转换图不停的跳来跳去..后来发现我太天真了..一个搞来搞去搞不清楚的bug是因为老师给的状态转换图坑了 - - 当然这肯定不怪老师因为我一开始做的时候居然没发现= =. 状态转换图我就暂且不贴了,另外两篇文章的地址如下: Compiler_词法分析_表驱动法 Compiler_词法分析_直接转向法 这次的是分析文件,用双缓冲.其实我也不是很清楚自己到底有没有用上双缓冲.. 下面简