日期:2014-05-20  浏览次数:20789 次

java jdbc连接SQL server2008数据库问题!在线等!!!谢谢!
Java code
package com.text;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.sql.*;
import java.awt.event.*;
public class text1 extends JFrame{

     Vector rowDate,columnNames;
     JTable jt=null;
     JScrollPane jsp=null;
     //定义数据库需要的
     PreparedStatement ps=null;
     Connection ct=null;
     ResultSet rs=null;
    public static void main(String[] args) {
    text1 tt=new text1();    

    }
    
    public text1()
    {
        columnNames=new Vector();
        columnNames.add("学号");
        columnNames.add("姓名");
        columnNames.add("性别");
        columnNames.add("年龄");
        columnNames.add("籍贯");
        columnNames.add("门派");
        
        rowDate=new Vector();
        System.out.println("ok1");
        try {
            //加载驱动
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=stuText");
            ps=ct.prepareStatement("select * from stu");
            rs=ps.executeQuery();
            System.out.println("ok2");
            while(rs.next())
            {
                Vector hang=new Vector();
                hang.add(rs.getString(1));
                hang.add(rs.getString(2));
                hang.add(rs.getString(3));
                hang.add(rs.getInt(4));
                hang.add(rs.getString(5));
                hang.add(rs.getString(6));
                rowDate.add(hang);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally
        {
            try {
                if(rs!=null)
                {
                    rs.close();
                }
                if(ps!=null) ps.close();
                if(ct!=null) ct.close();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }
        System.out.println("ok3");
        jt=new JTable(rowDate,columnNames);
        jsp=new JScrollPane(jt);
        this.add(jsp);
        this.setSize(400, 300);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }

}



sqljdbc4.jar包已引入 但是报这样的错误;
Java code
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.text.text1.<init>(text1.java:35)
    at com.text.text1.main(text1.java:17)

怎么回事啊??

------解决方案--------------------
你jar驱动文件放对位置了吗?

把你的ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=stuText");中的jdbc:microsoft:sqlserver://127.0.0.1:1433换成 jdbc:sqlserver://localhost:1433试试!

------解决方案--------------------
貌似没发现驱动
------解决方案--------------------
用户 'sa' 登录失败,驱动版本不一样,从这两个地方想想看,一般用户 'sa' 登录失败是不是密码不对呀,你再试试

------解决方案--------------------