各位大哥帮我看看为什么点击确认老是连不上 帮忙指点一下 是不是我那错了 代码如下:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.sql.*;
class MyRuanjian extends JPanel implements ActionListener{
JLabel lbUserName;
JTextField tfUserID;
JLabel lbUserpwd;
JPasswordField tfPwd;
JButton subimit;
JButton quit;
JFrame frame;
public MyRuanjian(){
lbUserName = new JLabel("用户工卡号");
tfUserID = new JTextField(10);
lbUserpwd = new JLabel("密码");
tfPwd = new JPasswordField(10);
subimit = new JButton("确认");
quit = new JButton("退出");
add(lbUserName);
add(tfUserID);
add(lbUserpwd);
add(tfPwd);
add(subimit);
add(quit);
subimit.addActionListener(this);
quit.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
int flag = 0;
String userName = tfUserID.getText();
String password = tfPwd.getText();
if(e.getSource() == quit ){
System.exit(0);
} else if(e.getSource() == subimit && userName !=null && password != null ){
try{
String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; //连接SQL数据库的方法
String URL="jdbc:microsoft:sqlserver://zxt:1433;DatabaseName=http"; //db_name为数据库名
String Username="sa"; //用户名
String Password=""; //密码
Class.forName(Driver); //加载数据可驱动
Connection con=DriverManager.getConnection(URL,Username,Password); //
Connection conn = null;
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql = "select * from Admin where userName='" + userName + "'AND password='"+ password +"'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next())
JOptionPane.showMessageDialog(null,"登陆成功","登陆",JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null,"登陆失败","警告",JOptionPane.WARNING_MESSAGE);
stmt.close();
conn.close();
}catch(ClassNotFoundException er){
JOptionPane.showMessageDialog(null,"没找到加栽的类","警告",JOptionPane.INFORMATION_MESSAGE);
}catch(SQLException err){
System.out.println("SQL错误提示:"+ err);
}
}
}
}
class MyFrame extends JFrame{
public MyFrame(){
super("登陆界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(200,150);
setSize(400,400);
this.getContentPane().setLayout(new FlowLayout());
Container contentPane = getContentPane();
contentPane.add(new MyRuanjian());
}
}
public class Login {
public static void main(String[] args){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new MyFrame();
frame.pack();
frame.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.sql.*;
class MyRuanjian extends JPanel implements ActionListener{
JLabel lbUserName;
JTextField tfUserID;
JLabel lbUserpwd;
JPasswordField tfPwd;
JButton subimit;
JButton quit;
JFrame frame;
public MyRuanjian(){
lbUserName = new JLabel("用户工卡号");
tfUserID = new JTextField(10);
lbUserpwd = new JLabel("密码");
tfPwd = new JPasswordField(10);
subimit = new JButton("确认");
quit = new JButton("退出");
add(lbUserName);
add(tfUserID);
add(lbUserpwd);
add(tfPwd);
add(subimit);
add(quit);
subimit.addActionListener(this);
quit.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
int flag = 0;
String userName = tfUserID.getText();
String password = tfPwd.getText();
if(e.getSource() == quit ){
System.exit(0);
} else if(e.getSource() == subimit && userName !=null && password != null ){
try{
String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; //连接SQL数据库的方法
String URL="jdbc:microsoft:sqlserver://zxt:1433;DatabaseName=http"; //db_name为数据库名
String Username="sa"; //用户名
String Password=""; //密码
Class.forName(Driver); //加载数据可驱动
Connection con=DriverManager.getConnection(URL,Username,Password); //
Connection conn = null;
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql = "select * from Admin where userName='" + userName + "'AND password='"+ password +"'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next())
JOptionPane.showMessageDialog(null,"登陆成功","登陆",JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null,"登陆失败","警告",JOptionPane.WARNING_MESSAGE);
stmt.close();
conn.close();
}catch(ClassNotFoundException er){
JOptionPane.showMessageDialog(null,"没找到加栽的类","警告",JOptionPane.INFORMATION_MESSAGE);
}catch(SQLException err){
System.out.println("SQL错误提示:"+ err);
}
}
}
}
class MyFrame extends JFrame{
public MyFrame(){
super("登陆界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(200,150);
setSize(400,400);
this.getContentPane().setLayout(new FlowLayout());
Container contentPane = getContentPane();
contentPane.add(new MyRuanjian());
}
}
public class Login {
public static void main(String[] args){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new MyFrame();
frame.pack();
frame.setVisible(true);
}
}
int flag = 0;
String userName = tfUserID.getText();
String password = tfPwd.getText();
if(e.getSource() == quit ){
System.exit(0);
} else if(e.getSource() == subimit && userName !=null && password != null ){
try{
改成:
public void actionPerformed(ActionEvent e){
String str=e.getActionCommand();
if (str.equals("确认"))
{}
试一试呗
userName 20
password 15
/*
String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; //SQL
String URL="jdbc:microsoft:sqlserver://zxt:1433; DatabaseName=http"; //db_name
String User="sa"; //
String Pass=""; //
Class.forName(Driver); //
Connection conn=DriverManager.getConnection(URL,User,Pass);
*/
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();/*************************************/
Connection conn=DriverManager.getConnection(
"jdbc:microsoft:sqlserver://zxt:1433;DatabaseName=http", "sa", "");
Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
String sql = "select * from Person_Main where ID='" + userName + "'AND password='"+ password +"'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
JOptionPane.showMessageDialog(null,"","",JOptionPane.INFORMATION_MESSAGE);
}else{
JOptionPane.showMessageDialog(null,"","",JOptionPane.WARNING_MESSAGE);
}
try {
if (rs != null) {
rs.close();
}
} catch (SQLException sqle) {
} catch (Exception e1) {
e1.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e3) {
e3.printStackTrace();
}}catch (Exception sqle) {/**********************************************/
sqle.printStackTrace();
}我用我的方法无问题