`
qinweiping
  • 浏览: 128354 次
  • 性别: Icon_minigender_1
  • 来自: 嘉兴
社区版块
存档分类
最新评论

【菜鸟学开发系统】学生成绩管理系统(三)

阅读更多

今天我们来探讨一下开发实现业务功能和数据库的操作 由于先前数据库设计的文档不小心弄丢了 正在写文章中(一)

package qinweiping;
import java.util.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ChangePwdTeacher extends JPanel 
                     implements ActionListener
{ 
    private String host;
    //声明Connection引用、Statement对象引用与结果集引用
	private Connection conn;
	private Statement stmt;
	private ResultSet rs;
	//创建信息提示标签数组
	private JLabel[] jlArray={new JLabel("用户名"),
	   new JLabel("原始密码"),new JLabel("新密码"),
	   new JLabel("确认新密码")};
	//创建用户名输入框
	private JTextField jtf=new JTextField();
	//创建密码框数组
	private JPasswordField[] jpfArray={new JPasswordField(),
	        new JPasswordField(),new JPasswordField()};
	//创建操作按钮
	private JButton[] jbArray={new JButton("确认"),new JButton("重置")};
/* 主要是完成了对该模块的所需控件的初始化工作,一些类似的控件可以用数组一次性创建 */

	//构造器                          
	public ChangePwdTeacher(String host)
	{ 
	    this.host=host; 
	   //初始化页面
		this.initialFrame();
		//为按钮注册监听器
		this.addListener();
	}


	public void addListener()
	{    //为文本框注册监听器
	     jtf.addActionListener(this);
	     //为密码框注册监听器
	     jpfArray[0].addActionListener(this);
	     jpfArray[1].addActionListener(this);
	     jpfArray[2].addActionListener(this);
	    //为按钮注册监听器
		jbArray[0].addActionListener(this);
		jbArray[1].addActionListener(this);
	}
	//初始化页面的方法
	public void initialFrame()
	{   //设为空布局
		this.setLayout(null);
		//将控件放入相应的位置
		for(int i=0;i<jlArray.length;i++)
		{
			jlArray[i].setBounds(30,20+50*i,150,30);
			this.add(jlArray[i]);
			if(i==0)
			{
				jtf.setBounds(130,20+50*i,150,30);
				this.add(jtf);
			}
			else
			{
				jpfArray[i-1].setBounds(130,20+50*i,150,30);
				this.add(jpfArray[i-1]);
			}
			
		}
		jbArray[0].setBounds(40,230,100,30);
		this.add(jbArray[0]);
		jbArray[1].setBounds(170,230,100,30);
		this.add(jbArray[1]);
		
	}


	//实现ActionListener接口中的方法
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==jtf)//输入用户名并回车后
		{
			jpfArray[0].requestFocus(true);
		}
		else if(e.getSource()==jpfArray[0])//输入原始密码并回车后
		{
			jpfArray[1].requestFocus(true);
		}
		else if(e.getSource()==jpfArray[1])//输入新密码并回车后
		{
			jpfArray[2].requestFocus(true);
		}
		else if(e.getSource()==jpfArray[2])//输入确认新密码并回车后
		{
			jbArray[0].requestFocus(true);
		}
/*主要实现键盘的易用性,用户可以通过按Enter建直接进入下一个输入框 而不用鼠标单击相应的输入框在进行文字的输入这样可以方便用户操作*/
		else if(e.getSource()==jbArray[1])
		{////按下重置按钮的处理代码
		     //将输入信息清空
			for(int i=0;i<jpfArray.length;i++)
			{
				jpfArray[i].setText("");
			}
			jtf.setText("");
		}
/*   用户单击“重置”按钮后之行动的动作 将输入框的内容全部清空*/
		else if(e.getSource()==jbArray[0])
		{//按下确认按钮的处理代码
		    //用于判断密码格式的正则式字符串
			String patternStr="[0-9a-zA-Z]{6,12}";
			String user_name=jtf.getText().trim();//获取用户名
			if(user_name.equals(""))
			{//用户名为空
				JOptionPane.showMessageDialog(this,"请输入用户名",
				                "错误",JOptionPane.ERROR_MESSAGE);
				return;
			}
			String oldPwd=jpfArray[0].getText().trim();//获取原始密码
			if(oldPwd.equals(""))
			{//原始密码为空
				JOptionPane.showMessageDialog(this,"请输入原始密码",
				                  "错误",JOptionPane.ERROR_MESSAGE);
				return;
			}
			String newPwd=jpfArray[1].getText().trim();//获取新密码
			if(newPwd.equals(""))
			{//新密码为空
				JOptionPane.showMessageDialog(this,"请输入新密码",
				                "错误",JOptionPane.ERROR_MESSAGE);
				return;
			}
			if(!newPwd.matches(patternStr))
			{//新密码不符合格式
				JOptionPane.showMessageDialog(this,
				                  "密码只能是6到12位的字母或数字",
				                  "错误",JOptionPane.ERROR_MESSAGE);
				return;
			}
			String newPwd1=jpfArray[2].getText().trim();//获取确认密码
			if(!newPwd.equals(newPwd1))
			{//新密码与确认密码不同
				JOptionPane.showMessageDialog(this,"确认密码与新密码不符",
				                       "错误",JOptionPane.ERROR_MESSAGE);
				return;
			}
/*  根据用户单击确认按钮时候执行动作 还可以判断用户输入的有没有逻辑问题*/
			try
			{   //初始化数据库连接并更改密码
				this.initialConnection();
				String sql="update user_teacher set pwd='"+newPwd+"'"+
				            " where uid='"+user_name+"'"+
				           " and pwd='"+oldPwd+"'";
				int i=stmt.executeUpdate(sql);
				if(i==0)
				{//更改失败
					JOptionPane.showMessageDialog(this,
					      "修改失败,请检查您的用户名或密码是否正确",
					      "错误",JOptionPane.ERROR_MESSAGE);
				}
				else if(i==1)
				{//更改成功
					JOptionPane.showMessageDialog(this,"密码修改成功",
					           "提示",JOptionPane.INFORMATION_MESSAGE);
				}
				this.closeConn();//关闭数据库连接
			}
			catch(Exception ea){
				ea.printStackTrace();
			}
		}
	}
	
/*  根据用户输入更新数据库操作,根据更新记录的条数来确定是否更新成功 如果是0就是更新失败 是1的话那就更新成功*/


public void setFocus()
	{
		jtf.requestFocus(true);
	}
	//自定义的初始化数据库连接的方法
	public void  initialConnection()
	{
		try
		{
			Class.forName("org.gjt.mm.mysql.Driver");
			conn=DriverManager.getConnection(
				 "jdbc:mysql://"+host+"/test","root","");
			stmt=conn.createStatement();
		}
		catch(SQLException e)
		{
e.printStackTrace();
		}
		catch(ClassNotFoundException e)
		{
			e.printStackTrace();
		}
	}
	//关闭数据库连接的方法
	public void closeConn()
	{
		try
		{
			if(rs!=null)
			{
				rs.close();
			}
			if(stmt!=null)
			{
				stmt.close();
			}
			if(conn!=null)
			{
				conn.close();
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
	}
	public static void main(String args[])
	{
		ChangePwdTeacher cpt=new ChangePwdTeacher("127.0.0.1:3306");
		JFrame jframe=new JFrame();
		jframe.add(cpt);
		jframe.setBounds(70,20,700,650);
		jframe.setVisible(true);
	}
}
【注】代码中用到了innitialConnection 和closeConn两个方法 他们是用来初始化数据库的连接和关闭的方法,这两个方法具体的实现在后面会给出 这两个方法在后面的很多模块要用到

 效果截图如下:



 

  • 大小: 6.6 KB
1
0
分享到:
评论
2 楼 qinweiping 2011-12-14  
greatwqs 写道
试试  MVC
恩 是的
1 楼 greatwqs 2011-12-14  
试试  MVC

相关推荐

Global site tag (gtag.js) - Google Analytics