码迷,mamicode.com
首页 > 其他好文 > 详细

01课堂测试

时间:2018-01-25 10:57:11      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:主机   message   enc   word   ansi   cep   port   ext   cat   

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

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课堂测试

标签:主机   message   enc   word   ansi   cep   port   ext   cat   

原文地址:https://www.cnblogs.com/liushiqiang123/p/8349747.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!