【VB】学生管理系统总结(四)

#1. 添加完信息之后清空文本框
'清空文本框信息
Dim ctrl As Control
'清空所有文本框中的内容(混用)

'方式一:(只能清空TEXT文本框中内容)
 For Each ctrl In Me.Controls
   If TypeOf ctrl Is TextBox Then    '判断是否为文本框textbox
     ctrl.Text = ""
   End If
 Next
'方式二:
    'txtSID.Text = ""
    'txtName.Text = ""
     comboSex.Clear
    'txtBorndate.Text = ""
     comboClassNo.Clear     
    'txtTel.Text = ""
    'txtRudate.Text  ""
    'txtAddress.Text = ""
    'txtComment.Text = ""

#2. 长度限制
'电话长度只能为11位

  If Len(txtTel) <> 11 Then
    MsgBox "电话号码为11位!", 64, "警告"
    txtTel.Text = ""
    txtTel.SetFocus
    Exit Sub
  End If

#3. 文本框中不能输入特殊字符

Private Sub txtSID_KeyPress(KeyAscii As Integer)
    If ((KeyAscii >= 33 And KeyAscii <= 47) Or (KeyAscii >= 58 And KeyAscii <= 64) Or (KeyAscii >= 91 And KeyAscii <= 96) Or (KeyAscii >= 123 And KeyAscii <= 126)) Then
      MsgBox "不能输入特殊字符,请重新输吧!", vbOKOnly + vbExclamation, "警告"
      KeyAscii = 0
      Exit Sub
    End If
End Sub

#4. 添加日历

‘需添加Monthview控件(选择:工程–部件–控件–Microsoft Windows CommonControls–2.6.0–添加MonthView控件)

Private Sub Form_Load()
  MonthView1.Visible = False
  MonthView2.Visible = False
End Sub
 
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
  txtBorndate.Text = MonthView1.Year & "-" & MonthView1.Month & "-" & MonthView1.Day
  MonthView1.Visible = False
End Sub
 
Private Sub MonthView2_DateClick(ByVal DateClicked As Date)
  txtRudate.Text = MonthView2.Year & "-" & MonthView2.Month & "-" & MonthView2.Day
  MonthView2.Visible = False
End Sub
 
Private Sub txtBorndate_Click()
  MonthView1.Visible = True
  MonthView2.Visible = False
End Sub
 
Private Sub txtRudate_Click()
  MonthView2.Visible = True
  MonthView1.Visible = False
End Sub

#5. 入学日期小于出生日期
'出生和入校时间

  Dim borndate As Date
  Dim getdate As Date
 
  borndate = Trim(txtBorndate.Text)
  getdate = Trim(txtRudate.Text)
  
  If getdate <= borndate Then
    MsgBox "入学时间不能小于出生时间,请重新输入!", vbOKOnly + vbInformation, "提示"
    txtRudate.SetFocus
    Exit Sub
  End If

#6. 登录对话框中
  对于这段代码:ok为全局变量,用户登录成功,OK被赋值为True,系统自动进入“创建学生管理系统的主窗体”一旦三次输入密码均不正确,OK被赋值为flase,公用模块的Main过程将根据OK的值决定是退出还是进入系统

#7. 使Tap键按顺序进行
主要应用控件的TABINDEX属性

txtSID.TabIndex = 0
  txtName.TabIndex = 1
  comboSex.TabIndex = 2
  txtBorndate.TabIndex = 3
  comboClassNo.TabIndex = 4
  txtRudate.TabIndex = 5
  txtTel.TabIndex = 6
  txtAddress.TabIndex = 7
  txtComment.TabIndex = 8
  cmdOK.TabIndex = 9
  cmdCancel.TabIndex = 10

#8. 设置窗体的大小和位置

    Me.Width = 7600
    Me.Height = 6700
    Me.Left = Screen.Width / 2 - Me.Width / 2
    Me.Top = Screen. Height / 2 - Me.Height / 2

#9. 查找新添加记录是否与已有记录重复
从数据库中查找已有记录存放到记录集中,记录集非空时,从第一条记录开始判断是否重复,若重复则exit sub;若不重复则继续判断下一条.

        txtSQL = "select* from user_info"
        Set mrc = ExecuteSQL(txtSQL,MsgText)
        While (mrc.EOF = False)
         If Trim$(mrc.Fields("user_name").value) = Trim$(TxtUserName.Text)Then
                  MsgBox "用户已存在,请重新输入用户名!",vbOKOnly + vbExclamation, "警告"
                  TxtUserName.SetFocus
                  TxtUserName.Text = ""
                  TxtPassword1.Text = ""
                  TxtPassword2.Text = ""
                  Exit Sub
        Else
                  mrc.MoveNext
        EndIf
        Wend

mrc.EOF = False说明mrc记录集不为空,经常用mrc.BOF和EOF的值判断记录集是否为空

#10. VB的程序窗口固定大小
更改窗体的BorderStyle属性为FixedSingle或Fixed Dialog或Fixed ToolWindow
也可以使用代码Me.BorderStyle = 1或 Me.BorderStyle = 3或 Me.BorderStyle = 4
一般使用FixedSingle,即 Me.BorderStyle = 1

#11. “0”
添加课程信息窗口出现了一个问题,数据库中该表中的课程编号的数据类型是int,当你输入001时,它自动保存的是1,虽然会有判断是否重复记录,但是出入的和保存的判断不出来,因为输入的是001,表中保存的是1,所以这样做的后果是表中会有重复的课程编号。原因在于VB中int类型保存时会自动舍去第一位是0的位数。

#12. 如何让combobox不能输入字符
combobox控件的Style属性为0

Private Sub comBograde_KeyPress(KeyAscii As Integer)
    KeyAscii = 0
End Sub
package cn.com.dao.chivementdao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import cn.com.util.DBConnection; import cn.com.util.DBSql; import cn.com.vo.chivementvo.ChivementVo; public class ExamDao { private Connection conn = DBConnection.getConnectionOracle(); private ChivementVo examVo; public ExamDao() { } public ExamDao(ChivementVo examVo) { super(); this.examVo = examVo; } /** * 全部查询 */ public Object[][] selectAll() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_ALL); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumberAll(DBSql.SELECT_ALL_COUNT)][max]; while (rs.next()) { for (int j = 0; j < max; j++) { date[i][j] = rs.getObject(j + 1); } i++; } // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 根据学号查询 */ public Object[][] selectBySid() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_BY_S_ID); ps.setInt(1, examVo.getS_id()); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumber(DBSql.SELECT_BY_S_ID_COUNT, examVo .getS_id())][max]; while (rs.next()) { for (int j = 0; j < max; j++) { date[i][j] = rs.getObject(j + 1); } i++; } // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 根据组号查询 */ public Object[][] selectByGid() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_BY_G_ID); ps.setInt(1, examVo.getG_id()); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumber(DBSql.SELECT_BY_G_ID_COUNT, examVo .getG_id())][max]; while (rs.next()) { for (int j = 0; j < max; j++) { date[i][j] = rs.getObject(j + 1); } i++; } // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 根据课程号查询 */ public Object[][] selectByCid() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_BY_C_ID); ps.setInt(1, examVo.getC_id()); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumber(DBSql.SELECT_BY_C_ID_COUNT, examVo .getC_id())][max]; while (rs.next()) { for (int j = 0; j < max; j++) { // System.out.println( examVo.getG_id()); date[i][j] = rs.getObject(j+1); } i++; } // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 根据姓名模糊查询 * * @return */ public Object[][] selectByName() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_BY_S_NAME); ps.setString(1, examVo.getS_name()); ps.setString(2, "%" + examVo.getS_name() + "%"); ps.setString(3, "%" + examVo.getS_name()); ps.setString(4, examVo.getS_name() + "%"); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumberByName(DBSql.SELECT_BY_S_NAME_COUNT, examVo.getS_name())][max]; while (rs.next()) { for (int j = 0; j < max; j++) { date[i][j] = rs.getObject(j + 1); } i++; } } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 根据课程名称模糊查询 * * @return */ public Object[][] selectByClassName() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_BY_CLASS_NAME); ps.setString(1, examVo.getClass_name()); ps.setString(2, "%" + examVo.getClass_name() + "%"); ps.setString(3, "%" + examVo.getClass_name()); ps.setString(4, examVo.getClass_name() + "%"); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumberByName(DBSql.SELECT_BY_CLASS_COUNT, examVo.getClass_name())][max]; while (rs.next()) { for (int j = 0; j < max; j++) { date[i][j] = rs.getObject(j + 1); } i++; } } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 修改选中学生的成绩 * */ public void updatSelectClass() { PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.UPDATE_EXAM_BY_STUID); ps.setInt(1, examVo.getClassExamChivement()); ps.setInt(2, examVo.getS_id()); ps.setInt(3, examVo.getC_id()); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } /** * 得到所有课程号和课程名 * * @return */ public String[] getClassNoName() { String[] classNoName = null; PreparedStatement ps = null; ResultSet rs = null; int j = 0; try { int i = getnumberAll(DBSql.SELECT_CLASS_NAME_COUNT); classNoName = new String[i + i]; ps = conn.prepareStatement(DBSql.SELECT_CLASS_NAME); rs = ps.executeQuery(); while (rs.next()) { classNoName[j] = rs.getString(1); classNoName[j + i] = rs.getString(2); j++; } } catch (SQLException e) { e.printStackTrace(); } return classNoName; } /** * 根据科目修改成绩 查询学号 姓名 成绩 * */ public void SelectClassStuName() { int i = 0; PreparedStatement ps = null; ResultSet rs = null; int j = getnumberBySelectClassName( DBSql.SELECT_CLASS_STU_SNO_SNAME_EXAM_COUNT, examVo.getC_id()); int[] sNum = new int[j]; String[] sName = new String[j]; int[] classExam = new int[j]; try { ps = conn.prepareStatement(DBSql.SELECT_CLASS_STU_SNO_SNAME_EXAM); ps.setInt(1, examVo.getC_id()); rs = ps.executeQuery(); while (rs.next()) { sNum[i] = rs.getInt(1); sName[i] = rs.getString(2); classExam[i] = rs.getInt(3); i++; } } catch (SQLException e) { e.printStackTrace(); } examVo.setSid(sNum); examVo.setSname(sName); examVo.setClassExam(classExam); } /** * 根据科目修改成绩 查询学号 姓名 成绩 后修改成绩 * */ public void UpdateClassStuName() { // int i = 0; PreparedStatement ps = null; ResultSet rs = null; int j = getnumberBySelectClassName( DBSql.SELECT_CLASS_STU_SNO_SNAME_EXAM_COUNT, examVo.getC_id()); try { ps = conn.prepareStatement(DBSql.UPDATE_CHIVEMENT_BY_CLASS); for(int i =0;i<j;i++){ ps.setInt(1, examVo.getClassExam()[i]); ps.setInt(2, examVo.getSid()[i]); ps.setInt(3, examVo.getC_id()); ps.executeUpdate(); } } catch (SQLException e) { e.printStackTrace(); } } /** * 全部查询获得行数 * * @return */ public int getnumberAll(String str) { int number = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(str); rs = ps.executeQuery(); rs.next(); number = rs.getInt(1); // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return number; } /** * 根据学号 根据组号 根据课程号查询 获得行数 * * @return */ public int getnumber(String str, int i) { int number = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(str); ps.setInt(1, i); rs = ps.executeQuery(); rs.next(); number = rs.getInt(1); // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return number; } /** * 根据姓名 课程名 查询 获得行数 * */ public int getnumberByName(String str, String i) { int number = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(str); ps.setString(1, i); ps.setString(2, "%" + i + "%"); ps.setString(3, "%" + i); ps.setString(4, i + "%"); rs = ps.executeQuery(); rs.next(); number = rs.getInt(1); } catch (SQLException e) { e.printStackTrace(); } return number; } // /** // * 根据课程名查询 获得行数 // * // */ // // public int getnumberByClassName(String str, String i) { // int number = 0; // PreparedStatement ps = null; // ResultSet rs = null; // try { // ps = conn.prepareStatement(str); // ps.setString(1, i); // ps.setString(2, "%" + i + "%"); // ps.setString(3, "%" + i); // ps.setString(4, i + "%"); // rs = ps.executeQuery(); // rs.next(); // number = rs.getInt(1); // } catch (SQLException e) { // e.printStackTrace(); // } // return number; // } /** * * 根据课程名修改成绩获得行数 * * @param str * @param i * @return */ public int getnumberBySelectClassName(String str, int i) { int number = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(str); ps.setInt(1, i); rs = ps.executeQuery(); rs.next(); number = rs.getInt(1); } catch (SQLException e) { e.printStackTrace(); } return number; } }
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值