软件工程概论作业03

该部分主要为 如何将程序结果存入数据库中:

1.前期jdbc简:

Java 数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。JDBC也是Sun Microsystems的商标。它JDBC是面向关系型数据库的。

  简单地说,就是用于执行SQL语句的一类Java API,通过JDBC使得我们可以直接使用Java编程来对关系数据库进行操作。通过封装,可以使开发人员使用纯Java API完成SQL的执行。

2.jdbc连接数据库代码:

<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

 

<title>My JSP 'index.jsp' starting page</title>

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="This is my page">

<!--www.mwcly.cn

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<formaction="fuction.jsp"method="post">

<tablewidth="400"align="center"border="1"cellpadding="0"cellspacing="0">

<tr><tdcolspan="2"align="center">向数据库插入信息测试</td></tr>

<tr><td>username:</td><td><inputtype="text"name="username"/></td></tr>

<tr><td>password:</td><td><inputtype="text"name="password"/></td></tr>

<tr><td>roleID:</td><td><inputtype="text"name="roleID"/></td></tr>

<tr><tdcolspan="2"align="center"><inputtype="submit"value="插入"/></td></tr>

</table>

</form>

</body>

</html>

第二个是fuction.jsp主要逻辑操作都在里面

<%@pagelanguage="java"import="java.util.*,java.sql.*"pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

<title>My JSP 'fuction.jsp' starting page</title>

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<%

     String username=request.getParameter("username");

     String passwd=request.getParameter("password");

int roleID=Integer.parseInt(request.getParameter("roleID"));

     String className="com.mysql.jdbc.Driver";

 String url="jdbc:mysql://localhost:3306/news";

 String user="root";

 String password="root";

 Connection conn; 

     Statement st;

Class.forName(className);

 

conn=DriverManager.getConnection(url, user, password);

 

String sql="INSERT INTO users(username,password,roleID) 

VALUES('"+username+"','"+passwd+"',"+roleID+")";

 

    st = (Statement) conn.createStatement();    // 创建用于执行静态sql语句的Statement对象   

 

int count = st.executeUpdate(sql);  // 执行插入操作的sql语句,并返回插入数据的个数   

if(count>0)

    {

    response.sendRedirect("success.jsp");

    }                   

    conn.close();   //关闭数据库连接   

 

%>

</body>

</html>

 

第三个成功页面success.jsp

<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

 

<title>My JSP 'success.jsp' starting page</title>

 

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="www.mwcly.cn,keyword2,keyword3">

<metahttp-equiv="description"content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

 

</head>

 

<body>

    成功插入数据!<ahref="index.jsp">返回继续</a>

</body>

</html>

 

4.项目计划日志:

5.事件记录日志

 

 

6.缺陷日志

 

posted @ 2017-03-17 20:20  发酸的丶蛋炒饭  阅读(132)  评论(0编辑  收藏  举报