开发目的:实现用户登录。 数据库:sqlserver2000 开发工具:Jcreator j2sdk1.42 代码如下: import java.sql.*; import java.applet.*; import java.awt.event.*; import java.awt.*; import javax.swing.*;
public class Login extends Applet implements ActionListener { String name,pwd; //JPanel panel1=new JPanel(); JButton confirmbtn=new JButton("Confrim"); JButton clearbtn=new JButton("Clear"); JButton cancelbtn=new JButton("Cancel"); JTextField nametext=new JTextField(20); JTextField pwdtext=new JTextField(20); public void init() { setLayout(new FlowLayout()); add(nametext); add(pwdtext); add(confirmbtn); add(clearbtn); add(cancelbtn); confirmbtn.addActionListener(this); clearbtn.addActionListener(this); cancelbtn.addActionListener(this); } public void start() { nametext.setText("Enter Your Registry Name"); pwdtext.setText("Enter Your Password"); } public void stop() { nametext.setEditable(false); pwdtext.setEditable(false); } public void actionPerformed(ActionEvent e) { if(e.getSource()==cancelbtn) start(); else if (e.getSource()==clearbtn) { nametext.setText(""); pwdtext.setText(""); } else if (e.getSource()==confirmbtn) { String name=nametext.getText(); String pwd=pwdtext.getText(); Conntosql(name,pwd); } } public void Conntosql(String rename,String repwd) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:login","sa","neusoft"); PreparedStatement stat=con.prepareStatement("select * from login where name=? and pwd=?"); stat.setString(1,rename); stat.setString(2,repwd); ResultSet rs=stat.executeQuery(); if (rs.getRow()<1) { JOptionPane.showMessageDialog(this,"Please reenter the message!"); start(); } else JOptionPane.showMessageDialog(this,"welcome!"); } catch(Exception cnfe) { System.out.print(cnfe); } } } 编译没有问题 我点击confirm的时候为什么程序一点反映都没有 在命令提示符下提示 连接被拒绝。这是怎么一回事呢?请指教