import java.sql.*;
import java.io.*;
public class DataBase {
/**
 * @param args
 */
public static void main(String[] args) 
{
// TODO Auto-generated method stub try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    }catch(ClassNotFoundException e)
      {
  System.out.println("got a error, as following");
  e.printStackTrace();
      }


    Connection conn=DriverManager.getConnectio("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=bossfish","sa","");
    Statement stmt=conn.createStatement();
    ResultSet rs=stmt.executeQuery("select * from book");
    while(rs.next())
    {
     System.out.println(rs.getString("computer science"));
    
    }
    rs.close();
    stmt.close();
    conn.close();
    
} }
--------------------------------------------------------------------------
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Unhandled exception type SQLException
Unhandled exception type SQLException
Unhandled exception type SQLException
Unhandled exception type SQLException
Unhandled exception type SQLException
Unhandled exception type SQLException
Unhandled exception type SQLException
Unhandled exception type SQLException at com.wgh.DataBase.main(DataBase.java:24)搞了老半天不晓得错在哪里,郁闷中...

解决方案 »

  1.   

    try
    {
    Class.forName("net.sourceforge.jtds.jdbc.Driver");
        }catch(ClassNotFoundException e)
          {
      System.out.println("got a error, as following");
      e.printStackTrace();
          }
        Connection conn=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433;DatabaseName=bossfish","sa"," ");
        Statement stmt=conn.createStatement();
       System.out.println(conn);
        stmt.close();
        conn.close();
    }
    运行结果:
    net.sourceforge.jtds.jdbc.ConnectionJDBC3@1050e1f
      

  2.   

    估计是你的用户名和密码有问题,可以使用sqlServer是数据库的导入导出功能进行验证下。驱动应该没问题。
      

  3.   

    你后面的关闭代码部分没有些try {}catch(){}啊,
    最后几句大妈都要抛异常的,是非运行时异常,一定要捕获或抛出的try{
        rs.close();
        stmt.close();
        conn.close(); 
    }catch(SQLException e){
     e.printStackTrace();
    }
      

  4.   

    我看你先简单的用
    public static void main(String[] args) throwx Exception进行测试好了,先专心在业务逻辑上。
    所有的SQL 操作,必须外面加上 SQLException 的 catch, 具体的参考4楼的那位。 
      

  5.   

    先这样测一下,来看看是否连接成功?
    在24行
    估计是rs取值的问题。
    你用的是select * from *** 那么必须把所有的值全部都用一遍。
    关闭连接是也要抛异常呀。