首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

java编写的登录界面连接SQL Server2008有关问题

2012-02-02 
java编写的登录界面连接SQL Server2008问题?源代码:import java.awt.EventQueueimport java.awt.event.Ac

java编写的登录界面连接SQL Server2008问题?
源代码:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.microsoft.sqlserver.jdbc.SQLServerConnection;

public class LoginForm extends JFrame {

/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginForm frame = new LoginForm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame
*/
public LoginForm() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 419, 308);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JLabel label = new JLabel();
label.setText("通讯录(测试版)");
label.setBounds(22, 25, 153, 38);
getContentPane().add(label);

final JLabel label_1 = new JLabel();
label_1.setText("账号:");
label_1.setBounds(53, 89, 53, 27);
getContentPane().add(label_1);

final JLabel label_2 = new JLabel();
label_2.setText("密码:");
label_2.setBounds(53, 144, 53, 27);
getContentPane().add(label_2);

final JTextField tf= new JTextField(10);

tf.setBounds(123, 89, 185, 27);
getContentPane().add(tf);

final JPasswordField ps= new JPasswordField(15);

ps.setBounds(125, 144, 183, 27);
getContentPane().add(ps);

final JButton button = new JButton();
button.setText("登录");
button.setBounds(86, 209, 106, 28);
getContentPane().add(button);
button.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
loginTip(tf, ps);
}

});

final JButton button_1 = new JButton();
button_1.setText("退出");
button_1.setBounds(233, 209, 106, 28);
getContentPane().add(button_1);
button_1.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dispose();
System.exit(0);
}

});
//
}
private void loginTip(JTextField tf,JPasswordField ps){
String user = tf.getText();
String password = ps.getText();

if(user.equals("")){
JOptionPane.showMessageDialog(null,"账号不能为空","Warning",JOptionPane.WARNING_MESSAGE);
return;
}
if(password.equals("")){
JOptionPane.showMessageDialog(null,"密码不能为空","Warning",JOptionPane.WARNING_MESSAGE);
return;
}
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://202.96.128.86,1433DatabaseName=Book";
String use = "book";
String passwor = "book";
Connection con = DriverManager.getConnection(url,use,passwor);
java.sql.Statement stmt=con.createStatement(); 
String sql = "select * FROM LoginFrom WHERE ZhangHaoMingCheng = '" + user + "' and Password = '" + password + "'"; 
ResultSet rs=stmt.executeQuery(sql);
if (rs.next()) { 
// TODO: 如果数据校验成功 显示主界面 并关闭登录界面 



JOptionPane.showMessageDialog(this, "成功登录", "提示", 
JOptionPane.INFORMATION_MESSAGE); 
this.dispose(); 

} else { 
JOptionPane.showMessageDialog(this, "帐号或密码错误!", "警告", 
JOptionPane.WARNING_MESSAGE); 
ps.requestFocus(); // 密码框选中 

}
catch(Exception e){
e.printStackTrace();

}

}

}


[解决办法]


[解决办法]
SQLSERVER服务开启就行了啊。。。cmd下键入services.msc在Windows服务中找到SQLSERVER的服务开启就行了。。。

热点排行