请高手帮忙指点一下,本人这东西不会,谢谢指教!!! 使用JAVA的awt或者swing包编写代码实现以下功能。要求按“登录”按钮则以数据库“chat DB"(sql server 2000)的用户表”中取出用户名和用户密码进行验证,按“取消”按钮关闭窗口,并退出应用程序请高手给出代码,谢谢!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我给出关键的代码:1.用listener监听button事件jButton2.addActionListener(new ActionListener{ public void actionPerformed(ActionEvent e) { QuerySQL(textField.getText()); }}2.访问SQL的语句:import java.sql.*;Connection conn=null;ResulstSet re=null;Statement stat=null;try{Class.forName("sun.jdbc.odbe.JdbcOdebDriver");String url="jdbc:odbc:YourDB";conn=DriverManager.getConnect(url);String sql="select * from db_name";rs=statement.executeQuery(sql);while(rs.next()){for(int i=1;i<col;i++) //此处的代码是为了取得一条记录的所有属性值, //所以用for。col是每条记录的属性值的个数String info=rs.getString(i); //info记录了此条记录的此列属性的信息}}catch(Exception e){} package util.gui.dlg;import javax.swing.*;import java.awt.event.*;import java.awt.*;import java.util.*;import java.sql.*;import oracle.jdbc.driver.*;public abstract class LoginDialog extends JDialog implements ActionListener , KeyListener{ // GUI elements private JTextField userNameTF = new JTextField(10); private JTextField passwordTF = new JPasswordField(10); private JButton okButton = new JButton( "确定" ); private JButton cancelButton = new JButton( "取消" ); // return value private boolean returnValue; private String userCode; public LoginDialog( JFrame f) { super( f , "登录窗口" , true ); addListener(); setGUI(); } public boolean isLegalUser() { return returnValue; } public String getUserCode() { return userCode; } /** * implements KeyListener */ public void keyPressed( KeyEvent e ) { if( e.getKeyCode() == KeyEvent.VK_ENTER ) { check(); } } public void keyReleased( KeyEvent e ) { } public void keyTyped( KeyEvent e ) { } /** * implements ActionListener */ public void actionPerformed( ActionEvent e ) { Object o = e.getSource(); if( o == okButton ) { check(); } if( o == cancelButton ) { LoginDialog.this.dispose(); System.exit( 0 ); }; } protected abstract boolean check( String userName , String password ); //---------------------------------------------------------- // private void setGUI() { JPanel mainPanel = new JPanel(); this.getContentPane().add( mainPanel ); GridLayout grid = new GridLayout( 3, 2 ); mainPanel.setLayout( grid ); mainPanel.add( new JLabel( "用户名:" ) ); mainPanel.add( userNameTF ); mainPanel.add( new JLabel( "密 码:" ) ); mainPanel.add( passwordTF ); mainPanel.add( okButton ); mainPanel.add( cancelButton ); pack(); Toolkit tk = Toolkit.getDefaultToolkit(); Dimension d = tk.getScreenSize(); int height = new Double( d.getHeight()/2 - getHeight()/2 ).intValue(); int width = new Double( d.getWidth()/2 - getWidth()/2 ).intValue(); setLocation( width , height ); setVisible( true ); } private void check() { if( isJTextFieldEmpty( userNameTF ) || isJTextFieldEmpty( passwordTF ) ) return; else { String userName = userNameTF.getText(); String password = passwordTF.getText(); returnValue = check( userName , password ); if( returnValue ) // login successful { userCode = userName; LoginDialog.this.dispose(); } else // login failed { passwordTF.setText( "" ); JOptionPane.showMessageDialog( this , "口令或密码错误!" ); } } } private void addListener() { okButton.addActionListener( this ); cancelButton.addActionListener( this ); userNameTF.addKeyListener( this ); passwordTF.addKeyListener( this ); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } private boolean isJTextFieldEmpty( JTextField tf) { String str = tf.getText(); if( str.equals( "" ) ) { JOptionPane.showMessageDialog( null , "文本框为空!" ); //tf.setFocusable( true ); return true; } return false; } public static void main( String[] args ) { JFrame f = new JFrame();System.out.println( "sdfsdf" ); //LoginDlg dlg = new LoginDlg(f);System.out.println( "sdfsdf" ); }} 上面是我写的一个登录对话框父类,用的时候只要覆盖protected abstract boolean check( String userName , String password );函数就可以了,比如 protected boolean check( String userName , String password ){ //SQL语句验证 return true;//return false} 如何用java判断 在window注册的服务名是否已经存在? 计算TableViewer的行数 如何实现类实例化时候实现接口 ant与junit结合时,想通过扩充ANT,通过读一个外部XML文件把文件中的值读到ANT已有的任务中 这段语句中的final起了什么作用? ZIP?? 今天去面试的一道题目大家看看对不! 请问有没有java tutorial 中文版 下载?用google都没找到。(八百里加急~) 考考同志们关于初始化的顺序的问题! 孙文琦同志请近来接分 寻求将从PPT转化为SWF的思路 java能写操作系统吗
1.用listener监听button事件
jButton2.addActionListener(new ActionListener{
public void actionPerformed(ActionEvent e) {
QuerySQL(textField.getText());
}
}2.访问SQL的语句:
import java.sql.*;
Connection conn=null;
ResulstSet re=null;
Statement stat=null;
try{
Class.forName("sun.jdbc.odbe.JdbcOdebDriver");
String url="jdbc:odbc:YourDB";
conn=DriverManager.getConnect(url);
String sql="select * from db_name";
rs=statement.executeQuery(sql);
while(rs.next()){
for(int i=1;i<col;i++) //此处的代码是为了取得一条记录的所有属性值,
//所以用for。col是每条记录的属性值的个数
String info=rs.getString(i); //info记录了此条记录的此列属性的信息
}
}catch(Exception e){}
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.sql.*;
import oracle.jdbc.driver.*;public abstract class LoginDialog extends JDialog implements ActionListener , KeyListener
{
// GUI elements
private JTextField userNameTF = new JTextField(10);
private JTextField passwordTF = new JPasswordField(10);
private JButton okButton = new JButton( "确定" );
private JButton cancelButton = new JButton( "取消" );
// return value
private boolean returnValue;
private String userCode;
public LoginDialog( JFrame f)
{
super( f , "登录窗口" , true );
addListener();
setGUI();
}
public boolean isLegalUser()
{
return returnValue;
}
public String getUserCode()
{
return userCode;
}
/**
* implements KeyListener
*/
public void keyPressed( KeyEvent e )
{
if( e.getKeyCode() == KeyEvent.VK_ENTER )
{
check();
}
}
public void keyReleased( KeyEvent e )
{
}
public void keyTyped( KeyEvent e )
{
}
/**
* implements ActionListener
*/
public void actionPerformed( ActionEvent e )
{
Object o = e.getSource();
if( o == okButton )
{
check();
}
if( o == cancelButton )
{
LoginDialog.this.dispose();
System.exit( 0 );
};
}
protected abstract boolean check( String userName , String password );
//----------------------------------------------------------
//
private void setGUI()
{
JPanel mainPanel = new JPanel();
this.getContentPane().add( mainPanel );
GridLayout grid = new GridLayout( 3, 2 );
mainPanel.setLayout( grid );
mainPanel.add( new JLabel( "用户名:" ) );
mainPanel.add( userNameTF );
mainPanel.add( new JLabel( "密 码:" ) );
mainPanel.add( passwordTF );
mainPanel.add( okButton );
mainPanel.add( cancelButton );
pack();
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
int height = new Double( d.getHeight()/2 - getHeight()/2 ).intValue();
int width = new Double( d.getWidth()/2 - getWidth()/2 ).intValue();
setLocation( width , height );
setVisible( true );
}
private void check()
{
if( isJTextFieldEmpty( userNameTF ) || isJTextFieldEmpty( passwordTF ) )
return;
else
{
String userName = userNameTF.getText();
String password = passwordTF.getText();
returnValue = check( userName , password );
if( returnValue ) // login successful
{
userCode = userName;
LoginDialog.this.dispose();
}
else // login failed
{
passwordTF.setText( "" );
JOptionPane.showMessageDialog( this , "口令或密码错误!" );
}
}
}
private void addListener()
{
okButton.addActionListener( this );
cancelButton.addActionListener( this );
userNameTF.addKeyListener( this );
passwordTF.addKeyListener( this );
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} private boolean isJTextFieldEmpty( JTextField tf)
{
String str = tf.getText();
if( str.equals( "" ) )
{
JOptionPane.showMessageDialog( null , "文本框为空!" );
//tf.setFocusable( true );
return true;
}
return false;
}
public static void main( String[] args )
{
JFrame f = new JFrame();
System.out.println( "sdfsdf" );
//LoginDlg dlg = new LoginDlg(f);
System.out.println( "sdfsdf" );
}
}
protected abstract boolean check( String userName , String password );
函数就可以了,比如
protected boolean check( String userName , String password )
{
//SQL语句验证
return true;//return false
}