一 实验目的

1熟悉myeclipse的使用。

2掌握MySQL的使用。

3掌握jdbc连接数据库。

二实验内容

1在MySQL下创建一个数据库students,并创建表student_info。并且输入记录。

2将mysql的jdbc的jar包导入myeclipse。

3编写注册用户界面的代码login.jsp和insert_stu_1.jsp和登录账号(用数据库中的记录)的代码tijiao.jsp和find.jsp。

三 实验工具

Myeclipse,mySql,和mysql-connector-java-5.0.4-bin.jar。mySql可以用可视化数据库管理工具,也可以用Dos界面下进行操作。45b3100dc5e3da85111b0f4a509032a2.png

可视化数据库管理工具Navicat

c51ec9075efeecafe9eb266d111e425b.png-wh_

10d6e8005b28eea9b97541160961bcb2.png-wh_

3d4b934a48999a780c08ce2b8ba2a961.png-wh_

Dos下mysql建立数据库和表的操作

信息注册界面

login.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>

  <head>

<title> 注册界面</title>


  </head>

  

  <body>欢迎来到注册界面,请填写你的信息!<hr width="100%" size="3"/>

   <form action="insert_stu_1.jsp" method="post">


<table border="0" width="238" height"252">

<tr><td>用户名</td><td><input type ="text" name="id"/></td></tr>

<tr><td>密    码</td><td><input type ="text" name="passwd"/></td></tr>

<tr align="center">

<td colspan="2">

<input type="submit" value="提     交">&nbsp;&nbsp;&nbsp;

<input type="reset" value="取      消">

</td></tr>

</table>

      </form>

  </body>

</html>

insert_stu_1.jsp:

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

<html>

<head><title>插入一条记录</title></head>

<body>

<%String driverName="com.mysql.jdbc.Driver";

String userName="root";

String userPwd="aaa312";

String dbName="students";

String url1="jdbc:mysql://localhost:3306/"+dbName;

String url2="?user="+userName+"&password="+userPwd;

String url3="&useUnicode=true&charsetEncoding=UTF-8";

String url=url1+url2+url3;

Class.forName(driverName);

Connection conn=DriverManager.getConnection(url);

String sql="Insert into students_info(id,passwd) values(?,?)";

PreparedStatement pstmt=conn.prepareStatement(sql);

request.setCharacterEncoding("UTF-8");

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

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

pstmt.setString(1,id);

pstmt.setString(2,passwd);

int n=pstmt.executeUpdate();

if(n==1){%>你的信息注册成功,欢迎登录!<br><%}else{ %>注册失败,请重试!<br><%} 

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

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

</body></html>

387262edcccffd1705b4c9e31c5de110.png-wh_

c105445e32656ef5867804e6c3d50a6d.png-wh_