改了一下你的代码,试试看,注意不要在取得SQL数据中用1,2,3这种形势,可以用字段名
import java.sql.*;public class UseSQLServer
{
public UseSQLServer(){ } public static void main(String[] args){
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver);
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName = pubs";
String user = "sa";
String password = "100";
Connection conn = DriverManager.getConnection(url,user,password);
Statement stmt = conn.createStatement();
String sql = "select * from jobs";
ResultSet rs = stmt.executeQuery(sql); while(rs.next()){
System.out.print("1: " + rs.getString(1) +" ");
System.out.print("2: " + rs.getString(2)+" ");
System.out.print("3: " + rs.getString(3)+" ");
System.out.println("4:" + rs.getString(4)+" ");
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception ex){
System.err.println(ex);
}
finally{
try{
if (rs!=null)
rs.close;
}catch(SQLException sqle){
}
try{
if (stmt!=null)
stmt.close;
}catch(SQLException sqle){
}
try{
if (conn!=null)
conn.close;
}catch(SQLException sqle){
}
}
}
}
import java.sql.*;public class UseSQLServer
{
public UseSQLServer(){ } public static void main(String[] args){
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver);
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName = pubs";
String user = "sa";
String password = "100";
Connection conn = DriverManager.getConnection(url,user,password);
Statement stmt = conn.createStatement();
String sql = "select * from jobs";
ResultSet rs = stmt.executeQuery(sql); while(rs.next()){
System.out.print("1: " + rs.getString(1) +" ");
System.out.print("2: " + rs.getString(2)+" ");
System.out.print("3: " + rs.getString(3)+" ");
System.out.println("4:" + rs.getString(4)+" ");
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception ex){
System.err.println(ex);
}
finally{
try{
if (rs!=null)
rs.close;
}catch(SQLException sqle){
}
try{
if (stmt!=null)
stmt.close;
}catch(SQLException sqle){
}
try{
if (conn!=null)
conn.close;
}catch(SQLException sqle){
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货