import java.sql.*;
/**
* JDBC访问SQL Server数据库测试
* @author 比尔德
*
*/
public class JDBCDemo
{
public static void main(String[] args)
{
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";
Connection con = null;
Statement st;
ResultSet rs;
try
{
Class.forName(driver).newInstance(); //装载数据库驱动程序
}
catch(Exception e)
{
e.printStackTrace();
}
//System.out.print("right...");
try
{
con = DriverManager.getConnection(url,"sa",null); //创建数据库连接实例
st = con.createStatement(); //设置查询语句
rs = st.executeQuery("select * from Student"); //保存结果集
while(rs.next()) //显示结果集
{
System.out.print("st_no = "+rs.getString(1)+" ");
System.out.print("st_name = "+rs.getString(2)+" ");
System.out.print("st_sex = "+rs.getString(3)+" ");
System.out.println("st_grade = "+rs.getString(1));
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(con==null) return;
try
{
con.close(); //关闭数据库连接
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
posted on 2006-04-27 12:48
比尔德 阅读(182)
评论(0) 编辑 收藏 所属分类:
java相关