最先创建一个实体包:entity,用于封装属性
我是在实体类包里面创建了一个houseInfo的java文件,代码如下:
package entity;
public class houseInfo {
private String hname;
private String htype;
private String hcontent;
private String htime;
private double hprice;
private String himage;
public String getHname() {
return hname;
}
public void setHname(String hname) {
this.hname = hname;
}
public String getHcontent() {
return hcontent;
}
public void setHcontent(String hcontent) {
this.hcontent = hcontent;
}
public String getHtype() {
return htype;
}
public void setHtype(String htype) {
this.htype = htype;
}
public String getHtime() {
return htime;
}
public void setHtime(String htime) {
this.htime = htime;
}
public double getHprice() {
return hprice;
}
public void setHprice(double hprice) {
this.hprice = hprice;
}
public String getHimage() {
return himage;
}
public void setHimage(String himage) {
this.himage = himage;
}
@Override
public String toString() {
return "houseInfo [hname=" + hname + ", htype=" + htype + ", hcontent=" + hcontent + ", htime=" + htime
+ ", hprice=" + hprice + ", himage=" + himage + "]";
}
}
通过sqlserver连接数据库
然后,创建一个连接SQL server数据库 的dao包,实现对数据库增删改操作方法
public class houseDao {
static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
static final String url = "jdbc:sqlserver://127.0.0.1:1433;database=houseDB;";
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<houseInfo> query1() throws SQLException{//对数据库进行查询
List<houseInfo>list=new ArrayList<houseInfo>();
String sql="select hname,htype,hcontent,htime,hprice,himage from houseInfo";
try(Connection conn=DriverManager.getConnection(url,"sa","123456");
PreparedStatement pstmt=conn.prepareStatement(sql)){
try(ResultSet rs = pstmt.executeQuery()){
while(rs.next()) {
houseInfo ci = new houseInfo();
houseInfo hi&