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

时间:2022-03-14 14:14:32
源代码:
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();

}

}

}

9 个解决方案

#1


补充一下:提示数据库连接问题?但我不知道怎么修改,请各位大侠帮忙一下。本人先在此谢了。

#2



String url = "jdbc:sqlserver://202.96.128.86,1433DatabaseName=Book";

url错误 改为"jdbc:sqlserver://202.96.128.86:1433;DatabaseName=Book";

#3


我在Eclipse下运行的,错误提示:
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 202.96.128.86, port 1433 has failed. Error: Connection timed out: connect. Please verify the connection properties and check that a SQL Server instance is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:130)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1195)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(SQLServerConnection.java:1054)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:758)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 202.96.128.86, port 1433 has failed. Error: Connection timed out: connect. Please verify the connection properties and check that a SQL Server instance is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:130)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1195)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(SQLServerConnection.java:1054)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:758)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)

#4



com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 202.96.128.86, port 1433 has failed

202.96.128.86这台机器的sqlserver数据库并没有开启吧。。端口1433没有打开。。

#5


怎么开启?

#6


SQLSERVER服务开启就行了啊。。。cmd下键入services.msc在Windows服务中找到SQLSERVER的服务开启就行了。。。

#7


我把SQL Server 代理(SQLEXPRESS)那项改为自动也不行。

#8


只做过mysql数据库的连接,帮顶!

#9


我的ip设置错了,我改后现在可以了,谢谢你们。

#1


补充一下:提示数据库连接问题?但我不知道怎么修改,请各位大侠帮忙一下。本人先在此谢了。

#2



String url = "jdbc:sqlserver://202.96.128.86,1433DatabaseName=Book";

url错误 改为"jdbc:sqlserver://202.96.128.86:1433;DatabaseName=Book";

#3


我在Eclipse下运行的,错误提示:
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 202.96.128.86, port 1433 has failed. Error: Connection timed out: connect. Please verify the connection properties and check that a SQL Server instance is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:130)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1195)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(SQLServerConnection.java:1054)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:758)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 202.96.128.86, port 1433 has failed. Error: Connection timed out: connect. Please verify the connection properties and check that a SQL Server instance is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:130)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1195)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(SQLServerConnection.java:1054)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:758)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)

#4



com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 202.96.128.86, port 1433 has failed

202.96.128.86这台机器的sqlserver数据库并没有开启吧。。端口1433没有打开。。

#5


怎么开启?

#6


SQLSERVER服务开启就行了啊。。。cmd下键入services.msc在Windows服务中找到SQLSERVER的服务开启就行了。。。

#7


我把SQL Server 代理(SQLEXPRESS)那项改为自动也不行。

#8


只做过mysql数据库的连接,帮顶!

#9


我的ip设置错了,我改后现在可以了,谢谢你们。