| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 816 人关注过本帖
标题:java连接sql2000报错
只看楼主 加入收藏
nicojjg
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2009-10-25
结帖率:0
收藏
已结贴  问题点数:20 回复次数:8 
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 
2009-10-27 23:54
nicojjg
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2009-10-25
收藏
得分:0 
还有一个实例:
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");
        }
         
    }
}
2009-10-27 23:55
nicojjg
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2009-10-25
收藏
得分:0 
执行后报错如下:

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
    at (URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at (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.(Component.java:5488)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
    at java.(Component.java:5253)
    at java.awt.Container.processEvent(Container.java:1966)
    at java.(Component.java:3955)
    at java.awt.Container.dispatchEventImpl(Container.java:2024)
    at java.(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.(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.(Component.java:5488)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
    at java.(Component.java:5253)
    at java.awt.Container.processEvent(Container.java:1966)
    at java.(Component.java:3955)
    at java.awt.Container.dispatchEventImpl(Container.java:2024)
    at java.(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.(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)
2009-10-27 23:56
nicojjg
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2009-10-25
收藏
得分:0 
请高手指点下~
2009-10-27 23:57
pywepe
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:296
专家分:483
注 册:2009-4-5
收藏
得分:20 
找不到驱动



java群
62635216
欢迎加入
2009-10-28 22:59
pywepe
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:296
专家分:483
注 册:2009-4-5
收藏
得分:0 
若用的是tomcat 你得把sql server的驱动的那个jar包放到 tomcat目录下的一个lib目录下去 (不能版本目录位置不相同)

java群
62635216
欢迎加入
2009-10-28 23:00
nicojjg
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2009-10-25
收藏
得分:0 
我用的是java,昨天又用连接sql2000的代码测试了一下,结果报错:no suitable driver,我的sql打的SP4补丁,昨天百度了一下,说要用SP3才行,我现在很困惑啊。。。
2009-10-29 08:32
windizual
Rank: 3Rank: 3
等 级:论坛游侠
威 望:4
帖 子:124
专家分:186
注 册:2009-7-1
收藏
得分:0 
回复楼上的,SP4肯定是可以的,我就用的SP4
你还是看一下你的驱动吧

Java要学的东西好多~~~~~
2009-11-05 13:55
sqjhwl
Rank: 2
等 级:论坛游民
帖 子:17
专家分:19
注 册:2009-7-24
收藏
得分:0 
我也遇到这个问题了

请教
2009-11-12 17:40
快速回复:java连接sql2000报错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.065969 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved