package zmsjdbc;
import java.sql.*;
public class sqlserver1
{
private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=catv";
private String user="sa";
private String pwd="";
public Connection getcon()
{
Connection con;
try
{ //加载驱动程序
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//创建连接
con=DriverManager.getConnection(url,user,pwd);
return con;
}
catch(ClassNotFoundException e)
{
System.out.println("加载驱动程序出错");
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
catch(Exception e)
{
System.out.println("出现了好大的错误");
}
return null;
}
public ResultSet executeSql(String sqlstr)
{
Connection conn;
Statement stmt;
try
{
conn=getcon();
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sqlstr);
return rs;
}
catch(SQLException e)
{
System.out.print("获取数据集时出现错误");
}
return null;
}
public static void main(String[] args)
{
ResultSet rs1;
sqlserver1 sql1=new sqlserver1();
rs1=sql1.executeSql("select ui_id,ui_name from userinfo");
try
{
while(rs1.next())
{
System.out.println(rs1.getString("ui_id")+" 姓名:"+rs1.getString("ui_name"));
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
/*11:08 2005-2-24 -by beyond
1:对于SQLSERVER 必须下载 SQLSERVER驱动
http://download.microsoft.com/download/3/0/f/30ff65d3-a84b-4b8a-a570-27366b2271d8/setup.exe
然后把 msbase.jar mssqlserver.jar msutil.jar 复制到 d:\jbuilder9/jdk1.4/lib/jre 下
2:如果出现错误:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
说明 SQLERVER 端口号 不对 》》》更改端口号:SQLSERVER->属性-常规-网络配置-常规-TCP/IP-属性--更改端口号
有个命令可以用: cmd-> netstat-a
例如:String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=zmstv";
说明你使用的是1433 在CMD 下 输入 netstat -a -n
应该看不到:TCP 127.0.0.1:1433 0.0.0.0:0 LISTENING
*/