3_32江_0519—MVC—MySQL简易增删改查测试
public class TextMySqlDbUtil {
@Test
public void textConnection(){
try {
MySqlDbUtil.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
public void textAdd(){
try {
String sql="insert into band (id,name,status) values (9,\"哈哈\",1)";
Connection conn = MySqlDbUtil.getConnection();
PreparedStatement pst = conn.prepareStatement(sql);
pst.execute();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
public void textDelete(){
try {
Connection conn = MySqlDbUtil.getConnection();
PreparedStatement pst = conn.prepareStatement("delete from band where id=9");
pst.execute();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
public void textUpdate(){
try {
Connection conn = MySqlDbUtil.getConnection();
PreparedStatement pst = conn.prepareStatement("UPDATE band332.band set name=22 WHERE id=2");
pst.execute();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
public void textFind(){
try {
Connection conn = MySqlDbUtil.getConnection();
PreparedStatement pst = conn.prepareStatement("select * from band");
ResultSet rs = pst.executeQuery();
while (rs.next()){
String id = rs.getString("id");
String name = rs.getString("name");
String status = rs.getString("status");
System.out.println("id:"+id+" name:"+name+" status:" + status);
}
pst.execute();
rs.close();
System.out.println();
} catch (SQLException e) {
e.printStackTrace();
}
}