TableName是类名
t_property是各个属性
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class TableName {
private int t_id;
private String t_property1;
private String t_property2;
private String t_property3;
private String t_property4;
private String t_property5;
private String t_property6;
public int getT_id() {
return t_id;
}
public void setT_id(int t_id) {
this.t_id = t_id;
}
public String getT_property1() {
return t_property1;
}
public void setT_property1(String t_property1) {
this.t_property1 = t_property1;
}
public String getT_property2() {
return t_property2;
}
public void setT_property2(String t_property2) {
this.t_property2 = t_property2;
}
public String getT_property3() {
return t_property3;
}
public void setT_property3(String t_property3) {
this.t_property3 = t_property3;
}
public String getT_property4() {
return t_property4;
}
public void setT_property4(String t_property4) {
this.t_property4 = t_property4;
}
public String getT_property5() {
return t_property5;
}
public void setT_property5(String t_property5) {
this.t_property5 = t_property5;
}
public String getT_property6() {
return t_property6;
}
public void setT_property6(String t_property6) {
this.t_property6 = t_property6;
}
// 根据传入的SQL语句返回一个对象的链表
public ArrayList<TableName> select(String sql) throws Exception {
ArrayList<TableName> result = new ArrayList<TableName>();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DB.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
TableName tableName = new TableName();
tableName.setT_id(rs.getInt("t_id"));
tableName.setT_property1(rs.getString("t_property1"));
tableName.setT_property2(rs.getString("t_property2"));
tableName.setT_property3(rs.getString("t_property3"));
tableName.setT_property4(rs.getString("t_property4"));
tableName.setT_property5(rs.getString("t_property5"));
tableName.setT_property6(rs.getString("t_property6"));
result.add(tableName);
}
} catch (SQLException sqle) {
throw new SQLException("select data exception: "
+ sqle.getMessage());
} catch (Exception e) {
throw new Exception("System e exception: " + e.getMessage());
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
throw new Exception("statement close exception: "
+ e.getMessage());
}
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e) {
throw new Exception("statement close exception: "
+ e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("statement close exception: "
+ e.getMessage());
}
}
return result;
}
// 根据传入的对象向数据库插入一条记录
public void insert(TableName tableName) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
// 根据对象的属性生成SQL语句
String sql = "insert into tableName(t_id,t_property1,t_property2,t_property3,t_property4,t_property5,t_property6) values('"
+ tableName.getT_id()
+ "','"
+ tableName.getT_property1()
+ "','"
+ tableName.getT_property2()
+ "','"
+ tableName.getT_property3()
+ "','"
+ tableName.getT_property4()
+ "','"
+ tableName.getT_property5()
+ "','"
+ tableName.getT_property6() + "')";
try {
conn = DB.getConnection();
ps = conn.prepareStatement(sql);
ps.executeUpdate();
} catch (SQLException sqle) {
throw new Exception("insert data exception:" + sqle.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
throw new Exception("ps close exception: " + e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("connection close exception:"
+ e.getMessage());
}
}
}
// 根据传入的对象更新数据库记录
public void update(TableName tableName) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
String sql = "update TableName set t_property1= '"
+ tableName.getT_property1() + "',t_property2="
+ tableName.getT_property2() + "',t_property3="
+ tableName.getT_property3() + "',t_property4="
+ tableName.getT_property4() + "',t_property5="
+ tableName.getT_property5() + "',t_property6="
+ tableName.getT_property6();
try {
conn = DB.getConnection();
ps = conn.prepareStatement(sql);
ps.executeUpdate();
} catch (SQLException sqle) {
throw new Exception("update exception: " + sqle.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
throw new Exception("ps close exception: " + e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("connection close exception: "
+ e.getMessage());
}
}
}
// 根据传入的对象删除数据库记录
public void delete(TableName tableName) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DB.getConnection();
// 根据对象的属性生成SQL查询语句
String sql = "delete form jobs where t_id=" + tableName.getT_id();
ps = conn.prepareStatement(sql);
ps.executeUpdate();
} catch (SQLException sqle) {
throw new Exception("delete data exception: " + sqle.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
throw new Exception("ps close exception " + e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("connection close exception: "
+ e.getMessage());
}
}
}
}