当前位置: 代码迷 >> Java相关 >> java连接sql2000报错
  详细解决方案

java连接sql2000报错

热度:358   发布时间:2009-10-27 23:54:22.0
java连接sql2000报错
我把代码都贴出来,请高手帮忙看下哪里出问题了,这段代码是复制过来的,以前可以执行,重新装过SQL和Myeclips之后就不行了,郁闷呀。。。
先发连接SQL的类:
package bao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Dao {
    Connection conn=null;//连接对象
    public Dao()//完成对连接对象的实例化
    {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=studb", "sa", "");
        } catch (ClassNotFoundException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
        
        
    }
   
    public boolean insert(String stuAcc,String stuPsw,String stuSex,String stuName)
    {
        boolean b=false;
          try {
            PreparedStatement pst = conn.prepareStatement("insert into stu values(?,?,?,?)");
              pst.setString(1, stuAcc);
              pst.setString(2,stuPsw);
              pst.setString(3, stuSex);
              pst.setString(4, stuName);
              int i=pst.executeUpdate();
              if(i>0)
              {
                  b=true;
              }
        } catch (SQLException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
        return b;
        
    }
}
搜索更多相关的解决方案: java  

----------------解决方案--------------------------------------------------------
还有一个实例:
package bao;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import bao.Dao;

public class StuIns extends JFrame {

    private JTextField txtName;
    private JTextField txtSex;
    private JTextField txtPsw;
    private JTextField txtAcc;
    JLabel lblInfo;
    /**
     * Launch the application
     * @param args
     */
    public static void main(String args[]) {
        try {
            StuIns frame = new StuIns();
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the frame
     */
    public StuIns() {
        super();
        getContentPane().setName("");
        getContentPane().setLayout(null);
        setBounds(100, 100, 468, 464);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(null, "插入", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        panel.setLayout(null);
        panel.setToolTipText("111");
        panel.setBounds(34, 37, 338, 369);
        getContentPane().add(panel);

        final JLabel lblAcc = new JLabel();
        lblAcc.setText("用户名");
        lblAcc.setBounds(57, 49, 60, 26);
        panel.add(lblAcc);

        final JLabel lblPsw = new JLabel();
        lblPsw.setText("密  码");
        lblPsw.setBounds(57, 97, 60, 26);
        panel.add(lblPsw);

        final JLabel lblSex = new JLabel();
        lblSex.setText("性  别");
        lblSex.setBounds(57, 141, 60, 26);
        panel.add(lblSex);

        final JLabel lblName = new JLabel();
        lblName.setText("姓  名");
        lblName.setBounds(57, 192, 60, 26);
        panel.add(lblName);

        txtAcc = new JTextField();
        txtAcc.setBounds(123, 50, 165, 26);
        panel.add(txtAcc);

        txtPsw = new JTextField();
        txtPsw.setBounds(123, 98, 165, 26);
        panel.add(txtPsw);

        txtSex = new JTextField();
        txtSex.setBounds(123, 142, 165, 26);
        panel.add(txtSex);

        txtName = new JTextField();
        txtName.setBounds(123, 193, 165, 26);
        panel.add(txtName);

        final JButton btnIns = new JButton();
        btnIns.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                butInsertActionPerformed(e);
            }
        });
        btnIns.setText("插入");
        btnIns.setBounds(125, 262, 99, 34);
        panel.add(btnIns);

        final JLabel lblInfo = new JLabel();
        lblInfo.setBounds(125, 315, 99, 26);
        panel.add(lblInfo);
         
    }
    protected void butInsertActionPerformed(ActionEvent e) {
        Dao studao=new Dao();
        String stuAcc=this.txtAcc.getText();
        String stuPwd=this.txtPsw.getText();
        String stuSex=this.txtSex.getText();
        String stuName=this.txtName.getText();
        boolean b=studao.insert(stuAcc, stuPwd, stuSex, stuName);
        if(b)
        {
            
            this.lblInfo.setText("插入ok");
        }
        else
        {
            
            this.lblInfo.setText("插入NG");
        }
         
    }
}
----------------解决方案--------------------------------------------------------
执行后报错如下:

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:164)
    at bao.Dao.<init>(Dao.java:13)
    at bao.StuIns.butInsertActionPerformed(StuIns.java:104)
    at bao.StuIns$1.actionPerformed(StuIns.java:91)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:5488)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
    at java.awt.Component.processEvent(Component.java:5253)
    at java.awt.Container.processEvent(Container.java:1966)
    at java.awt.Component.dispatchEventImpl(Component.java:3955)
    at java.awt.Container.dispatchEventImpl(Container.java:2024)
    at java.awt.Component.dispatchEvent(Component.java:3803)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
    at java.awt.Container.dispatchEventImpl(Container.java:2010)
    at java.awt.Window.dispatchEventImpl(Window.java:1778)
    at java.awt.Component.dispatchEvent(Component.java:3803)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at bao.Dao.insert(Dao.java:30)
    at bao.StuIns.butInsertActionPerformed(StuIns.java:109)
    at bao.StuIns$1.actionPerformed(StuIns.java:91)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:5488)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
    at java.awt.Component.processEvent(Component.java:5253)
    at java.awt.Container.processEvent(Container.java:1966)
    at java.awt.Component.dispatchEventImpl(Component.java:3955)
    at java.awt.Container.dispatchEventImpl(Container.java:2024)
    at java.awt.Component.dispatchEvent(Component.java:3803)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
    at java.awt.Container.dispatchEventImpl(Container.java:2010)
    at java.awt.Window.dispatchEventImpl(Window.java:1778)
    at java.awt.Component.dispatchEvent(Component.java:3803)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
----------------解决方案--------------------------------------------------------
请高手指点下~
----------------解决方案--------------------------------------------------------
找不到驱动



----------------解决方案--------------------------------------------------------
若用的是tomcat 你得把sql server的驱动的那个jar包放到 tomcat目录下的一个lib目录下去 (不能版本目录位置不相同)

----------------解决方案--------------------------------------------------------
我用的是java,昨天又用连接sql2000的代码测试了一下,结果报错:no suitable driver,我的sql打的SP4补丁,昨天百度了一下,说要用SP3才行,我现在很困惑啊。。。
----------------解决方案--------------------------------------------------------
回复楼上的,SP4肯定是可以的,我就用的SP4
你还是看一下你的驱动吧
----------------解决方案--------------------------------------------------------
我也遇到这个问题了

请教
----------------解决方案--------------------------------------------------------
  相关解决方案