//最简单的jdbc连接jdbc,记住5个步骤
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Test1 {
public static void main(String[] args) {
String url="jdbc:sqlserver://localhost:1433;DatabaseName=News2";
Connection con=null; //链接对象
try {
//1加载驱动类,实例化一下
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con=DriverManager.getConnection(url, "sa", "sa");
//2.获得连接对象
if(con!=null){
System.out.println("数据库链接成功!");
}
} catch (ClassNotFoundException e){
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
con.close();//关闭链接
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
在此基础上继续完善:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//数据库管理类做好了,可以直接链接+关闭语句对象+关闭链接对象;
public class DBManager {
private static Connection con=null;
private static String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String url="jdbc:sqlserver://localhost:1433;DatabaseName=News";
private static String sql="insert userInfo values('admin','admin',getdate())";
private static String user="sa";
private static String pwd="sa";
//1.公共方法是获得数据库链接对象
public static Connection getConnection(){
try {
Class.forName(driver);//加,连
con=DriverManager.getConnection(url,user,pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;//非void都需要return
}
//关闭所有方法;有3个参数!,省代码了!!!
public static void closeAll(ResultSet rs,Statement stmt,Connection con){
try {
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import java.sql.Date;
//实体类:存储介质
public class UserInfo {
private int id;
private String username;
private String pwd;
private Date createtime;
public UserInfo(int id, String username, String pwd, Date createtime) {
super();
this.id = id;
this.username = username;
this.pwd = pwd;
this.createtime = createtime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
}
import java.sql.*;
import java.sql.Date;
import java.util.*;
//对UserInfo表的数据库管理类
public class UserInfoDB {
//0.声明对象;
Connection con=null;
Statement stmt=null; //语句对象
PreparedStatement pstmt=null;
int result=0; //受影响的行数
ResultSet rs=null; //结果集对象
//这个方法:对UserInfo表的增 删 改方法
//sql:可以insert update delete
public int updateUserInfo(String sql){
try {
con=DBManager.getConnection();//加连了!!!
stmt=con.createStatement();//语句对象
result=stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBManager.closeAll(null, stmt, con);
}
return result;
}
//ArrayList:是个可变类型的动态集合
//针对预编译语句对象的公共方法;list:存放的是?参数的个数
public int updateUserInfo(String sql,List list){
try {
con=DBManager.getConnection();//嘉联
pstmt=con.prepareStatement(sql);//1.得对象
//2.要赋值;
for(int i=0;i<list.size();i++){
pstmt.setObject(i+1,list.get(i));
}
result=pstmt.executeUpdate();//3.去执行
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBManager.closeAll(null, pstmt, con);
}
return result;
}
//简单的查询
public List<UserInfo> getUser(String sql){
List<UserInfo>list=new ArrayList<UserInfo>();
try {
con=DBManager.getConnection();
pstmt=con.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()){
int id=rs.getInt(1);
String name=rs.getString(2);
String pwd=rs.getString("pwd");
Date date=rs.getDate(4);
UserInfo user=new UserInfo(id,name,pwd,date);
list.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBManager.closeAll(rs, pstmt, con);
}
return list;
}
}
测试类
import java.sql.*;
import java.util.*;
public class TestPre {
public static void main(String[] args) {
//测试;
UserInfoDB udb=new UserInfoDB();
String sql="select * from userInfo";
List<UserInfo>list=udb.getUser(sql);
System.out.println("编号\t用户\t密码\t日期");
for(UserInfo user:list){
System.out.println(user.getId()+"\t"+user.getUsername()+
"\t"+user.getPwd()+"\t"+user.getCreatetime());
}
}
}