什么应该是计数器的值,这样每次运行应用程序时它都会动态增加,而不会更改数据库

时间:2016-01-05 05:41:15

标签: java

计数器的值应该是每次运行应用程序时它会动态增加而不会更改数据库。

    @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();

        }
    }

0 个答案:

没有答案