最近一直在配置JAVA连接SQL SERVER 2000,但一直都无法正常运行!
请高手指点,要详细一些!!  谢谢!!!

解决方案 »

  1.   

    XP请打上sp3补丁
    将jdbc驱动导到你的classpath路径下
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"; 
    //pubs为你的数据库的 
    String user="sa"; //数据库用户名
    String password=""; //密码
    Connection conn= DriverManager.getConnection(url,user,password); 
    Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
      

  2.   

    com.microsoft.jdbc.sqlserver.SQLServerDriver
    驱动不能用,换最新的2005的驱动
      

  3.   

    package databasemanage;import databasemanage.AuthorBean;import java.sql.Connection;
    import java.sql.SQLException;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.PreparedStatement;
    import java.util.Vector;
    import java.io.IOException;
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.*;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.*;
    import org.w3c.dom.NodeList;
    public class SelectFromDatabase {
        String sql ;
        String url;
        String user ;
        String password ;
        Connection con = null ;
        PreparedStatement st = null;
        ResultSet rs = null ;    public SelectFromDatabase(){
            sql = "select * from authors" ;
            url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs" ;
            user = "sa" ;
            password = "cheng" ;
        }
        /**
         *
         * @return Connection
         * @throws SQLException
         */
        public Connection getConnection() throws SQLException{
            try {
                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
                con = DriverManager.getConnection(url , user ,password);
                return con;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
        /**
         *
         * @return Vector
         * @throws SQLException
         */    public Vector getRecords() throws SQLException{
            Vector v = new Vector();
            try {
                con = getConnection();
                st = con.prepareStatement(sql);
                rs = st.executeQuery();
                boolean tt = rs.next();
                while (tt){
                    AuthorBean authors = new AuthorBean();
                    authors.setAu_id(rs.getString(1));
                    authors.setAu_lname(rs.getString(2));
                    authors.setAu_fname(rs.getString(3));
                    authors.setPhone(rs.getString(4));
                    authors.setAddress(rs.getString(5));
                    authors.setCity(rs.getString(6));
                    authors.setState(rs.getString(7));
                    authors.setZip(rs.getString(8));
                    authors.setContract(rs.getString(9));                v.add(authors);
                    tt = rs.next();
                }            return v;
            } catch (Exception ex) {
                return null ;
            }
        }
        /**
         * use this method to write information to the *.txt files
         * create a *.xml files to hold the information
         * @param vAut Vector
         * @throws IOException
         * @throws Exception
         */
        public void writeIntoFiles( Vector vAut ) throws IOException , Exception{
            try {
                String uri = "E:\\";
                Vector v = new Vector();
                //File files = new File("info.txt");
                //System.out.println(files.getPath());
                FileWriter fw = new FileWriter("output.xml");
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write("<?xml version = \"1.0\"?>");
                bw.write("\n<Infomation>");            for (int i=0 ; i<vAut.size() ; i++){
                    AuthorBean au = (AuthorBean)vAut.elementAt(i);
                    bw.write("\n<info>\n");
                    if ( au.getAu_id()!= null ){
                        System.out.println("id" + au.getAu_id());
                        bw.write("<id>");
                        bw.write(au.getAu_id());
                        bw.write("</id>\n");
                    }
                    //bw.write(au.getAu_id());
                    //bw.newLine();
                    if ( au.getAu_lname()!= null ){
                        System.out.println("name:"+au.getAu_lname());
                        bw.write("<name>");
                        bw.write((i+1)+":  "+au.getAu_lname());
                        bw.write("</name>\n");
                    }
                    //bw.write((i+1)+":  "+au.getAu_lname());
                    //bw.newLine();
                    //bw.newLine();
                    bw.write("\n</info>");
                    bw.flush();
                    System.out.println("------------------------------");
                }
                bw.write("\n</Infomation>\n");
                bw.flush();            System.out.println("sucess to write into a file!");
                fw.close();
            } catch (Exception ex) {
                ex.printStackTrace();
                System.err.println(ex.getMessage());
            }
            finally{
            }
        }    public void readfromXMLFiles() throws IOException{
            try {
                String uri = "E:\\DatabaseManage(files management database management)\\classes\\databasemanage\\output.xml";
                String id="";
                String name="";
                DocumentBuilderFactory DBFactory = DocumentBuilderFactory.
                                                   newInstance();
                DocumentBuilder DbBuilder = DBFactory.newDocumentBuilder();
                org.w3c.dom.Document doc =  DbBuilder.parse(uri);
                org.w3c.dom.NodeList nodelist = doc.getElementsByTagName("info");
                for (int i=0 ; i<nodelist.getLength() ; i++){
                    System.out.println(i);
                    id=doc.getElementsByTagName("id").item(i).getFirstChild().getNodeValue();
                    name=doc.getElementsByTagName("name").item(i).getFirstChild().getNodeValue();
                    System.out.println("id为"+id+"的人的名字是"+name);
                }
            } catch (Exception ex) {
                //throw new InterruptedException(ex.getMessage());
            }    }    public static void main(String[] args) throws SQLException {
            SelectFromDatabase selectfromdatabase = new SelectFromDatabase();
            Vector vs = new Vector();
            String str=null ;
            AuthorBean[] aut;
            aut = new AuthorBean[100];
            int i = 0 ;
            int temp [] = null ;
            temp = new int[100];
            int temps ;        vs = selectfromdatabase.getRecords();        try {
                for (i=0 ; i<vs.size() ; i++){
                    aut[i] = (AuthorBean)vs.elementAt(i);
                    str = aut[i].getAu_id().substring(0,3);
                    temp[i] = Integer.parseInt(str);
                    System.out.println(i+":"+str);
                }            for (i=0 ; i<vs.size() ; i++){
                    for (int j = i + 1; j < vs.size(); j++) {
                        if ( temp[i] < temp[j])
                        {
                             temps =  temp[i];
                             temp[i] = temp[j];
                             temp[j] = temps;
                        }
                    }
                    System.out.println( temp[i]);
                }
                /*
                System.out.println("do you want to write the file?");
                BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
                if (in.equals("Y")||in.equals("y")){            }
                else if(in.equals("N")||in.equals("n")){            }
                else{
                    System.out.println("Sorry your Input Stream is invalidation:");
                    return ;
                }
                */           //call this method to write the infotmation to the files
               selectfromdatabase.writeIntoFiles( vs );
               selectfromdatabase.readfromXMLFiles();        } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println(ex.fillInStackTrace());
                System.out.println("catch errors:"+ex.getMessage());
            }
        }
    }