package java_jdbc;
//JDBC连接SQL Server
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/*
public class JdbcSQLServerTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//1.加载驱动
Connection conn = null; //连接对象
Statement stmt = null; //语句对象
ResultSet rs = null ; //数据集对象
String url = "jdbc:sqlserver://localhost:1433;databaseName=test";
String username = "sa";
String password = "123456";
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2.获得数据库连接
conn = DriverManager.getConnection(url, username, password);
/*
//判断数据库连接是否成功
if(conn!=null)
{
System.out.println("数据库连接成功!");
}
else
{
System.out.println("数据库连接失败!");
}*/
//3.创建语句对象
stmt = conn.createStatement();
String sql = "select * from users";
rs = stmt.executeQuery(sql);
while(rs.next())
{
System.out.println(rs.getInt("id")+","+rs.getString("username")+","+rs.getString("password"));
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if(rs!=null)
{
rs.close();
rs = null;
}
if(stmt!=null)
{
stmt.close();
stmt = null;
}
if(conn!=null)
{
conn.close();
conn = null;
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}
2.SQL Server数据库配置:
用户名为:sa
密码:123456
数据库名为:test
数据表名为:users
导入的架包为:sqljdbc4.jar
2025 - 快车库 - 我的知识库 重庆启连科技有限公司 渝ICP备16002641号-10
企客连连 表单助手 企服开发 榜单123