一、连接数据库 帮助类
package com.zking.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* 连接数据库 帮助类
* @author Administrator
*
*/
public class DBHelper {
//注册驱动
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (Exception e) {
e.printStackTrace();//处理异常信息
}
}
//连接语句
private static final String url = "jdbc:sqlserver://sqlserver:1433;DatabaseName=T283";
//获得连接
public static Connection getCon() {
Connection con = null;
try {
con = DriverManager.getConnection(url,"sa","123");
} catch (Exception e) {
e.printStackTrace();//处理异常信息
}
return con;
}
public static void close(Connection con,PreparedStatement ps,ResultSet rs) {
try {
if(con!=null) {
con.close();
}
if(ps!=null) {
ps.close();
}
if(rs!=null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();//处理异常信息
}
}
}
二、实体类
package com.zking.entity;
/**
* 学生 实体类
* 定义属性
* @author Administrator
*
*/
public class Student {
//定义属性,私有化属性
private int id;
private String name;
private String password;
private String sex;
//封装
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
//构造函数
public Student() {
super();
}
public Student(int id, String name, String password, String sex) {
super();
this.id = id;
this.name = name;
this.password = password;
this.sex = s