01课堂测试

Posted 肥鹅PU火

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了01课堂测试相关的知识,希望对你有一定的参考价值。

网站系统开发需要掌握的技术

html,CSS语法,XTML,XML和XSLM,客户端脚本,服务器端脚本,javascript语法,flash动画制作,GIF动画制作,网页图片处理,Photoshop,数据库设计

源代码:

package pers.sun.user;

public class User {

 private int id;
 private String userName;
 private String nickName;
 private String password;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getUserName() {
  return userName;
 }
 public void setUserName(String userName) {
  this.userName = userName;
 }
 public String getNickName() {
  return nickName;
 }
 public void setNickName(String nickName) {
  this.nickName = nickName;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 
}

package pers.sun.user;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import pers.sun.database.Database;
import pers.sun.exception.UserException;
public class UserTool implements UserInterface {

 @Override
 public void add(User user) {
  //获得链接对象
  Connection connect=Database.getConnection();
  //准备sql语句
  String sql="select count(*) from t_user where username=?";
  //语句传输对象;
  PreparedStatement prepared=null;
  ResultSet result=null;
  try {
      prepared=connect.prepareStatement(sql);
      prepared.setString(1, user.getUserName());
      //接受结果集
      result=prepared.executeQuery();
      //遍历结果集
      while(result.next()) {
       if(result.getInt(1)>0) {
        throw new UserException("用户已存在!");
       }
      }
      sql="insert into t_user(username,password,nickname) value (?,?,?)";
      prepared=connect.prepareStatement(sql);
      prepared.setString(1, user.getUserName());
      prepared.setString(2, user.getPassword());
      prepared.setString(3, user.getNickName());
      prepared.executeUpdate();
     
  } catch (SQLException e) {
   e.printStackTrace();
  }finally {
   Database.close(result);
   Database.close(prepared);
   Database.close(connect);
  }
 }

 @Override
 public void deleted(int id) {
  //获得链接对象
  Connection connect=Database.getConnection();
  String sql="delete from t_user where id = ?";
  PreparedStatement prepare=null;
  
  try {
   prepare=connect.prepareStatement(sql);
   prepare.setInt(1, id);
   prepare.executeUpdate();
  } catch (SQLException e) {
   e.printStackTrace();
  }finally {
   Database.close(prepare);
   Database.close(connect);
  }
  
 }

 @Override
 public void update(User user) {
  Connection connect=Database.getConnection();
  PreparedStatement prepare=null;
  String sql="update t_user set password=?,nickname=? where id ?";
  
  try {
   prepare=connect.prepareStatement(sql);
   prepare.setString(1, user.getPassword());
   prepare.setString(2, user.getNickName());
   prepare.setInt(3, user.getId());
   prepare.executeUpdate();
  } catch (SQLException e) {
   e.printStackTrace();
  }finally {
   Database.close(prepare);
   Database.close(connect);
  }
  
 }

 @Override
 public User lookup(String userName) {
  Connection connect=Database.getConnection();
  PreparedStatement prepare=null;
  ResultSet result=null;
  String sql="select * from t_user where username = ?";
     User user=new User();
  try {
   prepare=connect.prepareStatement(sql);
   prepare.setString(1, userName);
   result=prepare.executeQuery();
   while(result.next()) {
    user.setId(result.getInt("id"));
    user.setUserName(result.getString("username"));
    user.setPassword(result.getString("password"));
    user.setNickName(result.getString("nickname"));
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }finally {
   Database.close(result);
   Database.close(prepare);
   Database.close(connect);
  }
  
  return user;
 }

 @Override
 public User lookup(int id) {
  Connection connect=Database.getConnection();
  String sql="select * from t_user where id = ?";
  PreparedStatement prepare=null;
  ResultSet result=null;
  User user=new User();
  try {
   prepare=connect.prepareStatement(sql);
   prepare.setInt(1, id);
   result=prepare.executeQuery();
   while(result.next()) {
    user.setId(id);
    user.setUserName(result.getString("username"));
    user.setPassword(result.getString("password"));
    user.setNickName(result.getString("nickname"));
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }finally {
   Database.close(result);
   Database.close(prepare);
   Database.close(connect);
  }
  
  return user;
 }

 @Override
 public List<User> lookup() {
  Connection connect=Database.getConnection();
  String sql="select * from t_user ";
  PreparedStatement prepare=null;
  ResultSet result=null;
  List<User> users=new ArrayList<User>();
  User user=new User();
  try {
   prepare=connect.prepareStatement(sql);
   result=prepare.executeQuery();
   while(result.next()) {
    user.setId(result.getInt("id"));
    user.setUserName(result.getString("username"));
    user.setPassword(result.getString("password"));
    user.setNickName(result.getString("nickname"));
    users.add(user);
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }finally {
   Database.close(result);
   Database.close(prepare);
   Database.close(connect);
  }
  
  return users;
 }

}

package pers.sun.exception;

public class UserException extends RuntimeException{

 public UserException() {
  super();
  // TODO Auto-generated constructor stub
 }

 public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
  super(message, cause, enableSuppression, writableStackTrace);
  // TODO Auto-generated constructor stub
 }

 public UserException(String message, Throwable cause) {
  super(message, cause);
  // TODO Auto-generated constructor stub
 }

 public UserException(String message) {
  super(message);
  // TODO Auto-generated constructor stub
 }

 public UserException(Throwable cause) {
  super(cause);
  // TODO Auto-generated constructor stub
 }

}

package pers.sun.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Database {

 public static Connection getConnection() {
   //1.加载驱动
   try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
   } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
    e.printStackTrace();
   }
   String user="root";
   String password="sunyu";
   String url="jdbc:mysql://localhost:3306/user_massage";
   //建立连接 。
   //url格式:JDBC:子协议:子名称//主机名:端口/数据库名?属性名=属性值&…
   //2创建连接对象connection
   Connection connection=null;
   try {
       connection= DriverManager.getConnection(url, user , password);
   } catch (SQLException e) {
    e.printStackTrace();
   }
  return connection;
 }
 //3关闭资源
 //1关闭 连接对象
 public static void close(Connection connection) {
  
  try {
   if(connection != null) {
    connection.close();
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
 //2关闭 语句传输对象
 public static void close(PreparedStatement preparedStatement) {
  
  try {
   if(preparedStatement != null) {
    preparedStatement.close();
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
 //3关闭 结果集对象
 public static void close(ResultSet resultset) {
  
  try {
   if(resultset != null) {
    resultset.close();
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>用户添加页面</title>
</head>
<body>
     <form action="add.jsp" method="get">
          <table align="center" border ="1" width = "500">
               <tr>
                   <td>用户名称</td>
                   <td>
                       <input type="text" name="uesrname"/>
                   </td>
               </tr>
               <tr>
                 <td>用户密码</td>
                 <td>
                  <input type="password" name = "password"/>
                 </td>
               </tr>
               <tr>
                 <td>用户昵称</td>
                 <td>
                  <input type="text" name="nickname"/>
                 </td>
               </tr>
               <tr align="center">
                 <td colspan="1">
                    <input type="submit" value ="登陆"/>
                 </td>
               </tr>
          </table>
     </form>
</body>
</html>

以上是关于01课堂测试的主要内容,如果未能解决你的问题,请参考以下文章

软件工程系统开发课堂测试01

20155228 2018-01-03 课堂测试:嵌入式基础

JAVA语言课堂测试试卷01学生信息管理系统

JAVA语言课堂测试01源代码(学生成绩管理系统)

Java课堂测试01及感想

java_000.JAVA语言课堂测试试卷01