public class OrderTest {
private String driver="com.mysql.jdbc.Driver";
private String url="jdbc:mysql://localhost:3306/os?useUnicode=true&characterEncoding-utf8";
private String userName="root";
private String password="root";
private Connection connection;
@Test
public void addOrder(){
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection = DriverManager.getConnection(url,userName,password);
connection.setAutoCommit(false);
Statement statement = connection.createStatement();
statement.execute("insert into orders values('100001','100001',2,'2499',now(),null,null,'lidapeng','1300000000','河北承德','待发货')");
statement.execute("update products set stock=stock-2 where id='100001'");
statement.close();
connection.commit();
} catch (SQLException e) {
e.printStackTrace();
try {
connection.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
}finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
try {
connection.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
}