利用JDBC连接MS SQL Ser2000+sp2

来源:互联网 发布:专业流程图软件 编辑:程序博客网 时间:2024/05/27 19:26
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

&nbsp; 我在学习j2ee的时候使用JDBC连接MS SQL Server 2000,开始的时候一切都正常,但是后来遇到了一个很头痛的问题,就是我们在从表中读取数据放到ResultSet中后,读取列值的时候一定的按照列的顺序读取。

&nbsp; 比如如果表中有三列id, name, address,按条件查询得到的结果放到ResultSet中,显示结果时如果这样写代码:

&nbsp;

&nbsp; java.SQL.ResultSet employees;

&nbsp; while(employees.next()){

&nbsp; employees.getString("address");

&nbsp; employees.getString("name");

&nbsp; }

&nbsp; 在运行的时候将会"column can not be re-read"错误,此时把两条语句颠倒就好了(此条只适用于连接MS SQL Server 2000)

&nbsp; 连接数据库代码如下:

&nbsp;

&nbsp; try {

&nbsp; Class.forName("com.microsoft.JDBC.SQLServer.SQLServerDriver");

&nbsp; conn =

&nbsp; DriverManager.getConnection(

&nbsp; "JDBC:microsoft:SQLServer://localhost:1433;DatabaseName=Hotel;USer=sa;Password=sa");

&nbsp; // Class.forName("sun.JDBC.odbc.JDBCOdbcDriver");

&nbsp; // conn = DriverManager.getConnection("JDBC:odbc:hotel");

&nbsp; } catch (SQLException e) {

&nbsp; System.err.println(e);

&nbsp; } catch (ClassNotFoundException e) {

&nbsp; System.err.println(e);

&nbsp; }

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>