使用jdbc连接sqlserver数据库,需要添加的jdbc的jar包,下载地址为:http://www.microsoft.com/zh-cn/download/details.aspx?id=11774,并添加到工程中。
以下是连接selserver的源码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcDemo01 {
public static void main(String args[]){
String url="jdbc:sqlserver://localhost:1433;DatabaseName=AtmDB";
String user="sa";
String password="123456";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn=DriverManager.getConnection(url, user, password);
Statement stmt=conn.createStatement();
String sql="select * from t_user";
//stmt.execute(sql);
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString("id")+":"+rs.getString("username"));
}
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}