⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userid.java

📁 基于java语言编写且已编译运行的图书馆管理系统的开发应用。连接sql server 2005数据库
💻 JAVA
字号:
package untitled5;

import java.applet.*;
import java.sql.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import com.borland.jbcl.layout.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
//用户登录
public class userid  extends Applet {

  XYLayout xYLayout1 = new XYLayout();
  JLabel jLabel1 = new JLabel();
  JTextField jTextField1 = new JTextField();
  JLabel jLabel2 = new JLabel();
  JTextField jTextField2 = new JTextField();
  JButton jButton1 = new JButton();

  //Initialize the applet
  public void init() {
    jLabel1.setText("姓名");
    this.setLayout(xYLayout1);
    jTextField1.setText("");
    jLabel2.setText("密码");
    jButton1.setText("登录");
    jButton1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("ok");
        validID();
      }
    });
    Font font = new Font("宋体", Font.PLAIN, 12);
    add(jLabel1, new XYConstraints(13, 94, -1, 22));
    add(jTextField1, new XYConstraints(34, 94, 78, 25));
    add(jLabel2, new XYConstraints(114, 94, -1, 22));
    add(jTextField2, new XYConstraints(137, 94, 73, 25));
    add(jButton1, new XYConstraints(209, 94, -1, 26));
  }

  void validID() {
    try {
      String str1, str2;
      str1 = jTextField1.getText();
      str2 = jTextField2.getText();
     // System.out.println(str1);
      //System.out.println(str2);
      //装载jdbc驱动程序。
      //通过下面的程序我们完成对oracle数据库的动态连接。
      //装载jdbc驱动程序
      String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
      String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=student"; //连接服务器和数据库sample
      String userName = "sa"; //默认用户名
      String userPwd = "081129"; //密码
      //连接数据库
      Connection con;
      Class.forName(driverName);
      con=DriverManager.getConnection(dbURL, userName, userPwd);


      
      //执行动态的sql语句
      PreparedStatement pstmt = con.prepareStatement(
          //依据读者的名字查询读者的信息的sql语句
          " select name ,id,password from first where name=?");
      //将读者的名字添加到具体的sql语句中
      pstmt.setString(1, str1);
      //使用ResultSet中的方法executeQuery()来完成sql语句的执行
      ResultSet res = pstmt.executeQuery();
      //使用getString()来获取sql查询的结果
      //String sa=res.getString(1);
      if ((!res.next()) || res.getString("password") == null) {
        JOptionPane.showMessageDialog(this, "查无此人", "错误",
                                      JOptionPane.ERROR_MESSAGE);
      }else {
        if ( !res.getString("password").equals(str2)) {
          JOptionPane.showMessageDialog(this, "密码错误", "错误",
                                      JOptionPane.ERROR_MESSAGE);
        }else {
          System.out.println("zhengque");
          do{
            System.out.println(res.getString(1)+":" + res.getString(2) + ":" +res.getString(3));
          }while (res.next());
          loader();
        }
      }
      pstmt.close();
      con.close();

    }catch (ClassNotFoundException e) {
      System.out.println(e.getMessage());
    }catch (SQLException edd) {
      edd.printStackTrace() ;
      System.out.println(edd.getMessage());
    }
  }

  void loader() {
    try {
     Frame1   Frame= new Frame1();
       Frame.show();

    }
    catch (Exception ew) {

          System.out.println(ew.getMessage());
    }

  }

  class ValidFailedException
      extends SQLException {
    public ValidFailedException() {
    }

    public ValidFailedException(String reason) {
      super(reason);
    }
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -