用jsp、javabean做了一個學生信息管理系統,雖然j2ee的框架很多,但是基礎仍然很重要。麻雀雖小五臟俱全,希望本博客對j2ee初學者有幫助,也是對自己知識的復習和整合。
### 系統預覽:
### 管理員登錄界面

### 錄入信息界面

### 管理界面

### 修改學生信息

系統比較簡單,有增刪改查四個基本功能。
源碼下載地址:[學生信息管理系統源碼]
([http://download.csdn.net/detail/napoay/9411126](http://download.csdn.net/detail/napoay/9411126))
線上訪問地址:[http://120.27.46.201:8080/studentmanager](http://120.27.46.201:8080/studentmanager)(用戶名:admin,密碼:123456)
### 核心代碼
### jdbc連接數據庫
~~~
package cn.ac.ucas.conn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Conn {
public Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1/student?useUicode=true&characterEncoding=utf-8";
String user="root";
String password="123456";
conn=DriverManager.getConnection(url, user, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return conn;
}
}
~~~
### 管理員登陸
在數據庫中有一張user表,有用戶名和密碼兩個字段,登錄時將用戶輸入的用戶名和密碼和數據庫中的管理員畢竟,用戶名和密碼輸入正確登錄成功。
#### login.jsp
~~~
<jsp:useBean id="user" class="cn.ac.ucas.model.User"></jsp:useBean>
<jsp:useBean id="userservice" class="cn.ac.ucas.service.UserService"></jsp:useBean>
<jsp:setProperty property="*" name="user" />
<%
if (userservice.validUser(user)) {
session.setAttribute("user", user);
%>
<jsp:forward page="main.jsp"></jsp:forward>
<%
} else {
%>
<jsp:forward page="index.jsp"></jsp:forward>
<%
}
%>
~~~
#### 驗證用戶名和密碼
~~~
public boolean validUser(User user) {
try {
pstmt = conn.prepareStatement("select * from user where username=?and password=?");
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
ResultSet rst = pstmt.executeQuery();
if (rst.next()) {
return true;
} else {
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
~~~
### 使用session
用戶名和密碼輸入正確的時候把user放到seesion中:
~~~
session.setAttribute("user", user);
~~~
用戶名和密碼輸入不正確不能之間訪問登錄后的頁面,不然用戶直接輸入地址就可以訪問到管理頁面,登錄和不登錄沒有區別。下面試checklogin.jsp,需要登錄后訪問的頁面使用jsp:include包含在內,這樣如果session中沒有user信息返回到首頁,不能直接查看。
~~~
<%@page import="cn.ac.ucas.model.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
User user= (User)session.getAttribute("user");
if(user==null){
%>
<jsp:forward page="index.jsp"></jsp:forward>
<%
}
%>
~~~
退出時移出session
~~~
session.removeAttribute("user");
~~~
### 增
~~~
public boolean addStu(Student student) {
try {
pstmt = conn.prepareStatement(
"insert into Student(nickname,name,gender,birth,majority,course,interest,otherinfo)"
+ "values(?,?,?,?,?,?,?,?)");
pstmt.setString(1, student.getNickname());
pstmt.setString(2, student.getName());
pstmt.setByte(3, student.getGender());
pstmt.setString(4, student.getBirth());
pstmt.setString(5, student.getMajority());
pstmt.setString(6, student.getCourses());
pstmt.setString(7, student.getInterests());
pstmt.setString(8, student.getOtherinfo());
pstmt.executeUpdate();
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
~~~
### 刪
~~~
public boolean deleteStu(int id) {
try {
pstmt = conn.prepareStatement("delete from Student where id=?");
pstmt.setInt(1, id);
pstmt.executeUpdate();
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
~~~
### 改
~~~
public boolean updateStu(Student student){
try {
pstmt=conn.prepareStatement("update Student set nickname=?,name=?,gender=?,birth=?,majority=?,course=?,interest=?,otherinfo=? where id=?");
pstmt.setString(1, student.getNickname());
pstmt.setString(2, student.getName());
pstmt.setByte(3, student.getGender());
pstmt.setString(4, student.getBirth());
pstmt.setString(5, student.getMajority());
pstmt.setString(6, student.getCourses());
pstmt.setString(7, student.getInterests());
pstmt.setString(8, student.getOtherinfo());
pstmt.setInt(9, student.getId());
pstmt.executeUpdate();
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
~~~
### 查
#### 查詢所有學生信息
~~~
public List getAllStudent() {
List stuList = new ArrayList();
try {
pstmt = conn.prepareStatement("select * from Student");
ResultSet rst = pstmt.executeQuery();
while (rst.next()) {
Student stu = new Student();
stu.setId(rst.getInt(1));
stu.setNickname(rst.getString(2));
stu.setName(rst.getString(3));
stu.setGender(rst.getByte(4));
stu.setBirth(rst.getString(5));
stu.setMajority(rst.getString(6));
if (rst.getString(7) != null) {
stu.setCourse(rst.getString(7).split("&&"));
}
if (rst.getString(8) != null) {
stu.setInterest(rst.getString(8).split("&&"));
}
stu.setOtherinfo(rst.getString(9));
stuList.add(stu);
}
return stuList;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
~~~
#### 查詢單個學生信息
~~~
public Student getStuById(int id) {
try {
pstmt = conn.prepareStatement("select * from Student where id = ?");
pstmt.setInt(1, id);
ResultSet rst = pstmt.executeQuery();
if(rst.next()){
Student stu=new Student();
stu.setId(rst.getInt(1));
stu.setNickname(rst.getString(2));
stu.setName(rst.getString(3));
stu.setGender(rst.getByte(4));
stu.setBirth(rst.getString(5));
stu.setMajority(rst.getString(6));
if (rst.getString(7) != null) {
stu.setCourse(rst.getString(7).split("&&"));
}
if (rst.getString(8) != null) {
stu.setInterest(rst.getString(8).split("&&"));
}
stu.setOtherinfo(rst.getString(9));
return stu;
}
return null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
~~~
- 前言
- [J2EE]java web項目中調用word轉html命令行工具
- [J2EE]jsp項目中使用UEditor富文本編輯器
- [J2EE]The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- [j2ee]Eclipse搭建SSH開發框架
- Could not open Hibernate Session for transaction
- class org.springframework.web.context.ContextLoaderListener
- [java01]Java基本數據類型
- [java02]運算符
- jsp、javabean學生信息管理系統
- [java03]java字符串
- [ssh新聞發布系統一]搭建開發環境
- [ssh新聞發布系統二] 讀取新聞
- [ssh新聞發布系統三]存儲新聞
- [ssh新聞發布系統四]使用富文本編輯器發布新聞
- [ssh新聞發布系統五]刪除新聞
- struts2 helloworld
- struts請求走向流程
- [java04]java大數類