jdbc也就是数据库连接,话不多说,贴代码,也没什么可讲的
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLServer {
private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//数据库驱动
private static String dbUrl = "jdbc:sqlserver://127.0.0.1:1433; DatabaseName=USERDB";//数据库服务地址
private static String dbUserName ="zhansan";//用户名
private static String dbPassWord ="zhansan";//密码
public static void main(String[] args) {
Connection conn = null; //连接对象
//用于执行静态 SQL 语句并返回它所生成结果的对象。子接口:CallableStatement, PreparedStatement
Statement stmt = null;
//结果集
ResultSet rs = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(dbUrl,dbUserName, dbPassWord);
stmt = conn.createStatement();
rs = stmt.executeQuery("select id,name,pass from user");
while (rs.next()){
System.out.println(rs.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
} finally { //这里是关闭操作
try {
if (!conn.isClosed()) {
conn.close();
}
if (!stmt.isClosed()) {
stmt.close();
}
if (!rs.isClosed()) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}