58,452
社区成员
发帖
与我相关
我的任务
分享
public class Dbunit {
private static Connection conn = null;
private static Statement state = null;
private static ResultSet rst = null;
public static void addunit(Testunit t) {
try {
conn = getConnection();
state = conn.createStatement();
String sql = "insert into _unit values("+t.getNum()+",'"+t.getName()+"','"+t.getAdd()+"'," +
"'"+t.getGetdate()+"','"+t.getSenddate()
+"',"+t.getCost()+",'"+t.getAcceptor()+"','"+t.getAcceptdate()+"')";
//System.out.println(sql);
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
state.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static ArrayList<Testunit> lookforTestunits(String sql) {
ArrayList<Testunit> a = new ArrayList<Testunit>();
try {
conn = getConnection();
state = conn.createStatement();
// String o="select * from person where pname like '%"+s+"%'";
rst = state.executeQuery(sql);
//System.out.println(sql);
while (rst.next()) {
Testunit p = new Testunit();
p.setAcceptdate(rst.getString("acceptdate"));
p.setAcceptor(rst.getString("acceptor"));
p.setAdd(rst.getString("unitadd"));
p.setCost(rst.getFloat("testcost"));
p.setGetdate(rst.getString("date0"));
p.setName(rst.getString("unitname"));
p.setNum(rst.getInt("unitcardno"));
p.setSenddate(rst.getString("date1"));
a.add(p);
// System.out.println(p.getTitle() + " " + p.getCont());
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rst.close();
state.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return a;
}
public static Connection getConnection() {
Connection conn = null;
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String userPasswd = "mysql";
String dbName = "sampletest";
// String tableName="person";
String port = "3306";
String url = "jdbc:mysql://localhost:" + port + "/" + dbName + "?user="
+ userName + "&password=" + userPasswd
+ "&useUnicode=true&characterEncoding=utf-8";//
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}