use pubs;
create table myTable (fld1 int, fld2 varchar(20), remark text);
insert into myTable values (1, 'Joseph', 'Text for Joseph');
insert into myTable values (2, 'Michael', 'Text for Michael');
insert into myTable values (3, 'Thomas', 'Text for Thomas');
按一下 [查詢] 功能表 執行。
請結束部份的 Microsoft SQL 查詢分析。
建立和測試 Java 應用程式
將下列程式碼貼入 [記事本] 或您選擇的 t 分機編輯器中:
import java.sql.*;
import java.io.*;
public class Class1
{
public static void main (String[] args)
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver" );
Connection connection = DriverManager.getConnection("jdbc:microsoft:sqlserver://server:1433;databasename=pubs;SelectMethod=direct", "<UserId>","<PassWd>");
Statement stmt = connection.createStatement();
String s = "SELECT fld1, fld2, remark FROM myTable";
ResultSet rs = null;
rs = stmt.executeQuery(s);
System.out.println("Success! The following are the rows in this query:");
int fld1;
String fld2;
String remark;
while (rs.next())
{
remark = rs.getString("remark"); //fails if in this order
fld1 = rs.getInt("fld1");
fld2 = rs.getString("fld2");
//remark = rs.getString("remark"); //works fine if in this order
System.out.println(fld1 + ", " + fld2 + ", " + remark);
}
rs.close();
stmt.close();
connection.close();
}
catch(Exception e)
{
System.out.println(" Exception = " + e );
}
try
{
System.out.println("Press any key to quit...");
System.in.read();
}
catch (Exception e)
{
}
}
}
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。