July 16 2007

07月16日(星期一)

 

Java Swing ODBCT-Sql

=======================

Today'goal is achieve Login UserName is vaildate adn connecting with

DateBase (sql ) by ODBC

=======================

UserLoginFrm Source

=======================

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class UserLoginFrm extends JFrame {
public UserLoginFrm() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() {
getContentPane().setLayout(null);
jLabel1.setFont(new java.awt.Font("宋体", Font.PLAIN, 20));
jLabel1.setForeground(Color.red);
jLabel1.setText("密码修改");
jLabel1.setBounds(new Rectangle(141, 28, 92, 22));
jButton1.setBounds(new Rectangle(160, 242, 83, 25));
jButton1.setText("修改");
jButton1.addActionListener(new UserLoginFrm_jButton1_actionAdapter(this));
txtNewPassword.setBounds(new Rectangle(148, 181, 127, 21));
txtPassword.setBounds(new Rectangle(148, 131, 127, 21));
txtUserName.setBounds(new Rectangle(148, 87, 127, 21));
jLabel4.setText("用户新密码:");
jLabel4.setBounds(new Rectangle(44, 184, 80, 15));
jLabel3.setText("用户旧密码:");
jLabel3.setBounds(new Rectangle(44, 136, 80, 15));
this.getContentPane().add(jLabel2);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jLabel4);
this.getContentPane().add(txtUserName);
this.getContentPane().add(txtPassword);
this.getContentPane().add(txtNewPassword);
this.getContentPane().add(jButton1);
this.getContentPane().add(jLabel1);
jLabel2.setText("用户名:");
jLabel2.setBounds(new Rectangle(44, 90, 80, 15));
this.setTitle("用户密码更改");
this.setSize(600, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}

public static void main(String[] args) {
UserLoginFrm frm = new UserLoginFrm();
}

JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JTextField txtUserName = new JTextField();
JPasswordField txtPassword = new JPasswordField();
JPasswordField txtNewPassword = new JPasswordField();
JButton jButton1 = new JButton();
public void jButton1_actionPerformed(ActionEvent e) throws SQLException,
ClassNotFoundException {
String UserName = txtUserName.getText().trim();
String Password = String.valueOf(txtPassword.getPassword());
String NewPassword = String.valueOf(txtNewPassword.getPassword());
DBOperator dbo = new DBOperator();
if (dbo.IsUser(UserName, Password)) {
if (dbo.UserModify(UserName, NewPassword)) {
JOptionPane.showMessageDialog(this, "密码修改成功");

}
} else {
JOptionPane.showMessageDialog(this, "用户名与密码错误,请重新输入");
txtUserName.setText("");
txtUserName.requestFocus();
txtPassword.setText("");
txtNewPassword.setText("");

return;
}
}
}


class UserLoginFrm_jButton1_actionAdapter implements ActionListener {
private UserLoginFrm adaptee;
UserLoginFrm_jButton1_actionAdapter(UserLoginFrm adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {

try {
adaptee.jButton1_actionPerformed(e);
} catch (ClassNotFoundException ex) {
} catch (SQLException ex) {
}

}
}

===========================================

DBOperator Source

===========================================

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

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class DBOperator {
public DBOperator() {
}


public boolean IsUser(String UserName, String PasswordOld) {
String Resource = "sun.jdbc.odbc.JdbcOdbcDriver";
boolean flag = false;
try {
Class.forName(Resource);
try {
String sql =
"select count(*)as c from usertable where username='" +
UserName + "' and pwd='" + PasswordOld + "'";
Connection con = DriverManager.getConnection(
"jdbc:odbc:database", "sa", "");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if (rs != null) {
while (rs.next()) {
int temp = rs.getInt("c");
if (temp == 0) {
flag = false;
} else {
flag = true;
}
}
}
rs.close();
stmt.close();
con.close();
} catch (SQLException ex1) {
ex1.printStackTrace();
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}

return flag;
}

public boolean UserModify(String UserName, String PasswordNew) throws
ClassNotFoundException, SQLException {
boolean flag = false;
String Resource = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(Resource);
String sql = "update usertable set pwd='" + PasswordNew +
"' where username='" + UserName + "'";
Connection con = DriverManager.getConnection(
"jdbc:odbc:database", "sa", "");
Statement stmt = con.createStatement();

if (stmt.executeUpdate(sql) > 0) {
flag = true;
}
return flag;

}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值