Posted on 2008-06-25 22:25
陌上尘 阅读(427)
评论(0) 编辑 收藏 所属分类:
JAVA-J2ME 、
JAVA-J2EE
import java.sql.*;
public class Test {
public static void main(String[] args) {
String sql;
Connection conn;
Statement stmt; //声明Statement对象
ResultSet rs;
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=shopping";
String user="sa";
String password="sa";
conn= DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
sql="select * from Users";
rs=stmt.executeQuery(sql);
while(rs.next())
{
System.out.println(rs.getString(1)); //输出数据库第一列
System.out.println(rs.getString(2));
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
______________________________________________________________________________
import java.sql.*;
public class Test {
public static void main(String[] args) {
Connection conn;
Statement stmt;
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=shopping";
String user="sa";
String password="sa";
conn= DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
System.out.println(" 连接成功 !");
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}