1. 程式人生 > >問題與思考6

問題與思考6

order 數據庫鏈接 積極 jquery 鏈接 table 發生 array forname

網絡系統都需要哪些技術:

JSP基礎,JSP動作指令,JavaBean, JDBC, 表達試語言,國際化、標準標簽庫,MVC模式,數據庫鏈接

JSP用於顯示,servlet用於控制,JSP主要完成與用戶的交互過程,不應該包括處理代碼和控制代碼

課堂測試代碼:

package com.jaovo.msg.dao;

import java.util.List;

import com.jaovo.msg.model.User;

public interface IUserDao {

public void add(User user);

public void delete(int id);

public void update(User user);

public User load(int id);

public User load(String username);

public List<User> load();

}

package com.jaovo.msg.dao;

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 com.jaovo.msg.Util.DBUtil;

import com.jaovo.msg.Util.UserException;

import com.jaovo.msg.model.User;

//import sun.net.www.content.text.plain;

public class UserDaoImpl implements IUserDao {

@Override

public void add(User user) {

//鑾峰緱閾炬帴瀵矽薄

Connection connection = DBUtil.getConnection();

//鍑嗗sql璇彞

String sql = "select count(*) from t_user where username = ?";

//鍒涘緩璇彞浼犺緭瀵矽薄

PreparedStatement preparedStatement = null;

ResultSet resultSet = null;

try {

preparedStatement = connection.prepareStatement(sql);

preparedStatement.setString(1, user.getUsername());

//鎺ユ敹緇撴灉闆?

resultSet = preparedStatement.executeQuery();

//閬嶅巻緇撴灉闆?

while(resultSet.next()) {

if (resultSet.getInt(1) > 0) {

throw new UserException("用戶已經存在") ;

}

}

sql = "insert into t_user(username,password,nickname) value (?,?,?)";

preparedStatement = connection.prepareStatement(sql);

preparedStatement.setString(1, user.getUsername());

preparedStatement.setString(2, user.getPassword());

preparedStatement.setString(3, user.getNickname());

preparedStatement.executeUpdate();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

//鍏抽棴璧勬簮

DBUtil.close(resultSet);

DBUtil.close(preparedStatement);

DBUtil.close(connection);

}

}

@Override

public void delete(int id) {

Connection connection = DBUtil.getConnection();

String sql = "delete from t_user where id = ?";

PreparedStatement preparedStatement = null;

try {

preparedStatement = connection.prepareStatement(sql);

preparedStatement.setInt(1, id);

preparedStatement.executeUpdate();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

DBUtil.close(preparedStatement);

DBUtil.close(connection);

}

}

@Override

public void update(User user) {

Connection connection = DBUtil.getConnection();

//鍑嗗sql璇彞

String sql = "update t_user set password = ? , nickname=? where id = ?";

//鍒涘緩璇彞浼犺緭瀵矽薄

PreparedStatement preparedStatement = null;

try {

preparedStatement = connection.prepareStatement(sql);

preparedStatement.setString(1, user.getPassword());

preparedStatement.setString(2, user.getNickname());

preparedStatement.setInt(3, user.getId());

preparedStatement.executeUpdate();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

DBUtil.close(preparedStatement);

DBUtil.close(connection);

}

}

@Override

public User load(int id) {

Connection connection = DBUtil.getConnection();

//鍑嗗sql璇彞

String sql = "select * from t_user where id = ?";

//鍒涘緩璇彞浼犺緭瀵矽薄

PreparedStatement preparedStatement = null;

ResultSet resultSet = null;

User user = null;

try {

preparedStatement = connection.prepareStatement(sql);

preparedStatement.setInt(1, id);

resultSet = preparedStatement.executeQuery();

while(resultSet.next()) {

user = new User();

user.setId(id);

user.setUsername(resultSet.getString("username"));

user.setPassword(resultSet.getString("password"));

user.setNickname(resultSet.getString("nickname"));

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

DBUtil.close(resultSet);

DBUtil.close(preparedStatement);

DBUtil.close(connection);

}

return user;

}

@Override

public User load(String username) {

// TODO Auto-generated method stub

return null;

}

@Override

public List<User> load() {

Connection connection = DBUtil.getConnection();

//鍑嗗sql璇彞

String sql = "select * from t_user ";

//鍒涘緩璇彞浼犺緭瀵矽薄

PreparedStatement preparedStatement = null;

ResultSet resultSet = null;

//闆嗗悎涓彧鑳芥斁鍏ser瀵矽薄

List<User> users = new ArrayList<User>();

User user = null;

try {

preparedStatement = connection.prepareStatement(sql);

resultSet = preparedStatement.executeQuery();

while(resultSet.next()) {

user = new User();

user.setId(resultSet.getInt("id"));

user.setUsername(resultSet.getString("username"));

user.setPassword(resultSet.getString("password"));

user.setNickname(resultSet.getString("nickname"));

users.add(user);

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

DBUtil.close(resultSet);

DBUtil.close(preparedStatement);

DBUtil.close(connection);

}

return users;

}

}

package com.jaovo.msg.model;

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 com.jaovo.msg.Util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

public class DBUtil {

public static Connection getConnection() {

try {

//1 鍔犺澆椹卞姩

Class.forName("com.mysql.jdbc.Driver").newInstance();

} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

String user = "root";

String password = "LQYroot";

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

Connection connection = null;

try {

//2 鍒涘緩閾炬帴瀵矽薄connection

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

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return connection;

}

//鍏抽棴璧勬簮鐨勬柟娉?

public static void close(Connection connection ) {

try {

if (connection != null) {

connection.close();

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void close(PreparedStatement preparedStatement ) {

try {

if (preparedStatement != null) {

preparedStatement.close();

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void close(ResultSet resultSet ) {

try {

if (resultSet != null) {

resultSet.close();

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

package com.jaovo.msg.Util;

public class UserException extends RuntimeException{

/**

*

*/

private static final long serialVersionUID = 1L;

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

}

}

<%@ 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 lang="en">

<head>

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>

<title>用戶添加頁面</title>

<style type="test/css">.box{

width:700 px;

height:352 px;

margin:0 auto;

backgrand:url(bbb.PNG)) no-repeat center;

}</style>

</head>

<body>

<div class="box"></div>

<%=request.getAttribute("error") %>

<form action="add.jsp" method="get">

<table align="center" border="1" width="500">

<tr>

<td>用戶名稱 :</td>

<td>

<input type="text" name="username" />

</td>

</tr>

<tr>

<td>用戶密碼:</td>

<td>

<input type="password" name="password" />

</td>

</tr>

<tr>

</tr>

<tr align="center">

<td colspan="2">

<input type="submit" value="登錄" />

</td>

</tr>

</table>

</form>

</body>

</html>

<%@page import="com.jaovo.msg.Util.UserException"%>

<%@page import="com.jaovo.msg.dao.UserDaoImpl"%>

<%@page import="com.jaovo.msg.model.User"%>

<%@ 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>

<%

//接收客戶端傳遞過來的參數

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

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

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

if(username == null || "".equals(username.trim())){

request.setAttribute("error", "用戶名不能為空");

%>

<jsp:forward page="addInput.jsp"></jsp:forward>

<%

}

User user = new User();

user.setUsername(username);

user.setPassword(password);

user.setNickname(nickname);

UserDaoImpl userDao = new UserDaoImpl();

try{

//UserDaoImpl userDao = new UserDaoImpl();

userDao.add(user);

//重定向

%>

<%

}catch(UserException e){

%>

<h2 style="color:red ; font-size:50px">發生錯誤 : <%=e.getMessage() %></h2>

<%

}

%>

</html>

運行截圖:

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

說明課堂測試未按時完成的原因:

沒有提前學習數據庫方面的知識,上課時沒有認真記筆記,記錄eclipse的配置方法,課下也沒有認真學習JSp方面的知識,總的來說自主學習能力不足。

對這門課的希望和目標;

實踐中學習,實踐中總結,鍛煉獨立解決問題的能力,勤於總結,善於合作,積極學習。

計劃每周有25小時的學習該門課程的時間。

問題與思考6