Swing图形界面程序—同学通讯录系统(1)

一个简单的Swing程序(通讯录)

    来东软一个月了,前天学了一个上午的Swing,老师要求做一个通讯录图形界面程序,就试着做了做,还有许多不完善的地方,如果有时间的话,后面还会接着改的,开发工具是MyEclipse,数据库使用的是Oracle数据库。首先把界面都摆出来吧!

   登录界面

Swing图形界面程序—同学通讯录系统(1)_第1张图片

   1.用户界面

Swing图形界面程序—同学通讯录系统(1)_第2张图片

  2.用户添加自己同学信息

Swing图形界面程序—同学通讯录系统(1)_第3张图片

3.查询自己的同学信息

Swing图形界面程序—同学通讯录系统(1)_第4张图片

4.删除同学信息

Swing图形界面程序—同学通讯录系统(1)_第5张图片

5.修改个人密码

Swing图形界面程序—同学通讯录系统(1)_第6张图片

管理员界面

Swing图形界面程序—同学通讯录系统(1)_第7张图片

1.管理员添加用户

Swing图形界面程序—同学通讯录系统(1)_第8张图片

2.删除用户(非管理员用户)

Swing图形界面程序—同学通讯录系统(1)_第9张图片

接下来是数据库表的设计,就两张表,一个用户表,一个学生表

--用户信息表
create table user_info(
       userid number primary key,  --用户编号
       username varchar(10 char) not null, --用户姓名
       passwd varchar(32 char) not null,  --用户密码
       priv varchar(10) default 'common' not null check (priv in('common','director')), --用户权限  --1.admin 2.普通用户
       registertime date default sysdate not null,
       intime date default sysdate not null,
       outitme date default sysdate not null
);
create table u_student
(
       sno number primary key,
       sname varchar(10 char) not null,
       sex char(2) default '男' not null check (sex in('男','女')),
       birthday date,
       classno varchar(8 char) not null,
       tel varchar(20 char) ,
       email varchar(20 char),
       address varchar(40 char),
       userid number not null,
       foreign key (userid) references user_info(userid)    
);

这里就只要注意一下学生表的userid和用户的userid之间的关系,在用户删除那里要同时删除两张表中的数据,使用的是存储过程。

----存取过程----用来删除两张表中的数据---
create or replace procedure pro_s_u_userid(in_userid number) is
begin
  delete from u_student where userid = in_userid;
  delete from user_info where userid = in_userid;
end;


call pro_s_u_userid(1040);


对学生表的操作类:StudentService.class


package com.edu.services;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.edu.db.DBUtils;

public class StudentService {
	
	private Connection conn = null;
	private PreparedStatement pstm = null;
	private ResultSet rs = null;
	
	public boolean del(String sno,String userid) throws Exception{
		try{
			conn = DBUtils.getConnectiOn();
			String sql = "DELETE FROM U_STUDENT WHERE SNO = ? AND USERID = ?";
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, sno);
			pstm.setString(2, userid);
			return pstm.executeUpdate() > 0;
		} finally{
			DBUtils.close(pstm);
			DBUtils.close(conn);
		}
	}
	
	/**
	 * 通过sno查询
	 * @param sno
	 * @return
	 * @throws Exception
	 */
	public List<String []> queryForDel(String sno,String userid) throws Exception{
		try{
			conn = DBUtils.getConnectiOn();
			StringBuilder sql = new StringBuilder()
				.append("SELECT S.SNO,S.SNAME,S.SEX,S.BIRTHDAY,S.CLASSNO,")
				.append("		S.TEL,S.EMAIL,S.ADDRESS  FROM U_STUDENT S ")
				.append(" WHERE USERID = ? ");
			if (sno != null && !sno.equals("")) {
				sql.append(" AND S.SNO = ? ");
			}
			sql.append(" ORDER BY S.SNAME");
			pstm = conn.prepareStatement(sql.toString());
			pstm.setString(1, userid);
			if (sno != null && !sno.equals("")) {
				pstm.setString(2, sno);
			}
			rs = pstm.executeQuery();
			List<String []> list = new ArrayList<String[]>();
			String val [] = null;
			int count = 0;
			while(rs.next()){
				val = new String [] {
						String.valueOf(++count),
						rs.getString(1),
						rs.getString(2),
						rs.getString(3),
						rs.getString(4),
						rs.getString(5),
						rs.getString(6),
						rs.getString(7),
						rs.getString(8)
				};
				list.add(val);
			}
			return list;
		} finally{
			DBUtils.close(rs);
			DBUtils.close(pstm);
			DBUtils.close(conn);
		}
	}
	
	
	
	/**
	 * 模糊查询姓名
	 * @param username
	 * @return
	 * @throws Exception
	 */
	public List<String []> query(String username,String userid) throws Exception{
		try{
			conn = DBUtils.getConnectiOn();
			StringBuilder sql = new StringBuilder()
				.append("SELECT S.SNAME,S.SEX,TO_CHAR(S.BIRTHDAY,'YYYY-MM-DD'),S.CLASSNO,")
				.append("       S.TEL,S.EMAIL,S.ADDRESS")
				.append("  FROM U_STUDENT S")
				.append(" WHERE USERID = ? ");
			if (username != null && !username.equals("")) {
				sql.append(" AND S.SNAME LIKE ?");
			}
			sql.append(" ORDER BY S.SNAME");
			pstm = conn.prepareStatement(sql.toString());
			pstm.setObject(1, userid);
			if (username != null && !username.equals("")) {
				pstm.setObject(2, "%" + username + "%");
			}
//			System.out.println("SQL : " + sql.toString());
			rs = pstm.executeQuery();
			
			List<String []> list = new ArrayList<String[]>();
			String val [] = null;
			int count = 0;
			
			while(rs.next()){
				val = new String []{
					String.valueOf(++count),
					rs.getString(1),
					rs.getString(2),
					rs.getString(3),
					rs.getString(4),
					rs.getString(5),
					rs.getString(6),
					rs.getString(7)
				};
				list.add(val);
//				System.out.println("list of service : " + list);
			}
			return list;
		} finally{
			DBUtils.close(rs);
			DBUtils.close(pstm);
			DBUtils.close(conn);
		}
	}
	
	/**
	 * 添加
	 * @param val
	 * @return
	 * @throws Exception
	 */
	public boolean add(String...val) throws Exception{
		try {
			conn = DBUtils.getConnectiOn();
			StringBuilder sql = new StringBuilder()
				.append("INSERT INTO U_STUDENT(SNO,SNAME,SEX,BIRTHDAY,CLASSNO")
				.append("						,TEL,EMAIL,ADDRESS,USERID)")
				.append(" 			   VALUES (S_U_STUDENT_SNO.NEXTVAL,?,?,TO_DATE(?,'YYYY-MM-DD'),")
				.append("						?,?,?,?,?)");
//			System.out.println("SQL :" + sql.toString());
			pstm = conn.prepareStatement(sql.toString());
			int size = val.length;
			for (int i = 0; i < size; i++) {
				pstm.setObject(i + 1, val[i]);
			}
			return pstm.executeUpdate() > 0;
		} finally{
			DBUtils.close(pstm);
			DBUtils.close(conn);
		}
	}

}

对用户表的操作类:UserInfoService.class

package com.edu.services;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.edu.db.DBUtils;

public class UserInfoServices {
	
	private Connection conn = null;
	private PreparedStatement pstm = null;
	private ResultSet rs = null;
	private CallableStatement cstm = null;
	
	public String queryPriv(String userid) throws Exception{
		try{
			conn = DBUtils.getConnectiOn();
			StringBuilder sql = new StringBuilder()
			.append("SELECT PRIV")
			.append("  FROM USER_INFO U")
			.append(" WHERE U.USERID = ?");
			pstm = conn.prepareStatement(sql.toString());
			pstm.setObject(1, userid);
//			System.out.println("-" + userid + "-");
			rs = pstm.executeQuery();
			String priv = null;
			if(rs != null){
				while(rs.next())
				{
					priv = rs.getString(1);
				}
			}
			return priv;
		}finally{
			DBUtils.close(rs);
			DBUtils.close(pstm);
			DBUtils.close(conn);
		}
	}
	
	public void delUser(String userid) throws Exception{
		try {
			conn = DBUtils.getConnectiOn();
			String sql = "{call pro_s_u_userid(?)}";
			cstm = conn.prepareCall(sql);
			cstm.setString(1, userid);
			cstm.execute();
		} finally{
			DBUtils.close(cstm);
			DBUtils.close(conn);
		}
	}
	
	public boolean addUser(String username,String passwd,String priv) throws Exception{
		try{
			conn = DBUtils.getConnectiOn();
			StringBuilder sql = new StringBuilder()
			.append("INSERT INTO USER_INFO(USERID,USERNAME,PASSWD,PRIV,REGISTERTIME)")
			.append("       VALUES (S_USER_INFO_USERID.NEXTVAL,?,?,?,SYSDATE)");
			pstm = conn.prepareStatement(sql.toString());
			pstm.setString(1, username);
			pstm.setString(2, passwd);
			pstm.setString(3, priv);
			return pstm.executeUpdate() > 0;
		} finally{
			DBUtils.close(pstm);
			DBUtils.close(conn);
		}
	}
	
	public boolean updatepwd(String userid,String pwd,String newpwd) throws Exception{
		try{
			conn = DBUtils.getConnectiOn();
			StringBuilder sql = new StringBuilder()
			.append("UPDATE USER_INFO U")
			.append("   SET U.PASSWD = ?")
			.append(" WHERE U.USERID = ?")
			.append("   AND U.PASSWD = ?");
			pstm = conn.prepareStatement(sql.toString());
			pstm.setString(1, newpwd);
			pstm.setString(2, userid);
			pstm.setString(3, pwd);
			return pstm.executeUpdate() > 0;
		} finally{
			DBUtils.close(pstm);
			DBUtils.close(conn);
		}
	}
	
	/**
	 * 用户登录
	 * @param username
	 * @param passwd
	 * @param priv
	 * @return
	 * @throws Exception
	 */
	public String login(String username,String passwd,String priv) throws Exception{
		try {
			conn = DBUtils.getConnectiOn();
			StringBuilder sql = new StringBuilder()
				.append("SELECT U.USERID ")
				.append("  FROM USER_INFO U ")
				.append(" WHERE U.PASSWD = ? ")
				.append("   AND U.PRIV =? ")
				.append("   AND U.USERNAME = ? ");
			pstm = conn.prepareStatement(sql.toString());
			pstm.setObject(1, passwd);
			pstm.setObject(2, priv);
			pstm.setObject(3, username);
			rs = pstm.executeQuery();
			String userid = null;
			while(rs.next()){
				userid = rs.getString(1);
			}
			return userid;
		} finally{
			DBUtils.close(rs);
			DBUtils.close(pstm);
			DBUtils.close(conn);
		}
	}
	
}
接下来只需要在对应的按钮执行对应的方法就行了!

一个简单的Swing程序就完成了!

源码链接:源码下载

你可能感兴趣的:(java,oracle,swing,通讯录,图形)