计数器的值应该是每次运行应用程序时它会动态增加而不会更改数据库。
@Component
public class JdbcDaoImpl {
static PreparedStatement ps;
static int counter = 0;
ResultSet rs;
ResultSet rs2;
Connection conn = null;
private Connection getConnection() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException{
if(conn==null)
{
try {
String driver = "org.apache.derby.jdbc.ClientDriver";
Class.forName(driver).newInstance();
conn = DriverManager
.getConnection("jdbc:derby://localhost:1527//db2");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
return conn;
}
public Circle getCircle(int circleId) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
// Circle circle= null;
//int instance = 0;
try {
conn = getConnection();
ps = conn.prepareStatement("SELECT * FROM circle where id =?");
ps.setInt(1, circleId);
Circle circle = null;
rs = ps.executeQuery();
if (rs.next()) {
circle = new Circle(circleId, rs.getString("name"));
}
if(circle!=null)
{
counter++;
ps= conn.prepareStatement("UPDATE circle set name=? where id=?");
String name=("dav"+(counter));
ps.setString(1, name);
ps.setInt(2, circleId);
ps.executeUpdate();
conn.commit();
}
return circle;
} finally {
rs.close();
ps.close();
conn.close();
}
}