Eclipse J2SE JDBC问题
登陆框输入密码后报错。我把1433端口已经开了,SQL2005驱动也装了。
java.lang.Exception: 数据库驱动未找到到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
at jdbcfile.<init>(jdbcfile.java:25)
at Login$ActionHandler.actionPerformed(Login.java:62)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6216)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5981)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class Login extends JFrame {
/**
* @param args
*/
private JLabel JLb1;
private JLabel JLb2;
private JButton OK_btn;
private JButton Cancel_btn;
private JTextField jtflduser;
private JPasswordField jtpwdfld;
private JFrame frame;
public Login(){
frame=new JFrame("登陆");
Container content =frame.getContentPane();
content.setLayout(new GridLayout(3,2,20,20));
JLb1=new JLabel("用户名");
JLb2=new JLabel("密码");
JLb1.setHorizontalAlignment(SwingConstants.CENTER);
JLb2.setHorizontalAlignment(SwingConstants.CENTER);
jtflduser=new JTextField();
jtpwdfld=new JPasswordField();
OK_btn=new JButton("确定");
Cancel_btn=new JButton("取消");
OK_btn.addActionListener(new ActionHandler());
Cancel_btn.addActionListener(new ActionHandler());
content.add(JLb1);
content.add(jtflduser);
content.add(JLb2);
content.add(jtpwdfld);
content.add(OK_btn);
content.add(Cancel_btn);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setSize(300,200);
frame.setVisible(true);
}
class ActionHandler implements ActionListener{
public void actionPerformed(ActionEvent e)
{
String str1,str2,sqlStr;
Object obj=e.getSource();
str1=jtflduser.getText().trim();
str2=new String(jtpwdfld.getPassword()).trim();
try{
if(obj.equals(OK_btn))
{
if(str1.equals("")){
JOptionPane.showMessageDialog(frame,"用户名不能为空!");
return;
}
jdbcfile conn=new jdbcfile();
sqlStr="select * from Admin where num=' "+str1+" 'and password=' "+str2+" '";
ResultSet result=conn.executeQuery(sqlStr);
if(result.next()){
JOptionPane.showMessageDialog(frame,"登陆成功!");
bookmain bookmain1=new bookmain();
bookmain1.go();
frame.dispose();
conn.close();
}else{
JOptionPane.showMessageDialog(frame,"用户名或密码错误");
}
}else if(obj.equals(Cancel_btn)){
System.exit(0);
}
}
catch(ClassNotFoundException ce)
{
System.out.println("SQLException:"+ce.getMessage());
}
catch(SQLException ex)
{
System.out.println(ex);
}
catch(Exception s)
{
s.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Login login =new Login();
}
}
[解决办法]
sqlStr="select * from Admin where num=' "+str1+" 'and password=' "+str2+" '";
单独写普通的java测试是否正确。在两个''里面可能存在空格。正确的用户名和密码多了个空格在前面或后面就不等了。连接失败。
[解决办法]
你到百度上去看下,Connection refused: connect
的原因
[解决办法]
你看看你的用户名密码有没有在SQLSERVER中安全性中设置过?可以设为sysadmin权限
[解决办法]
做记号,明天来弄~
[解决办法]
应该是你的jar包没有导入完,只引进了一部分,SQL2000要引入三个jar包, 2005也应该不止一个
[解决办法]
我也碰到类似的问题过,坐等高手
[解决办法]
我也遇到过是sysadmin权限问题