- 论坛徽章:
- 0
|
package com.Myself.database.calculate;
//查询数据库中某表的metrix,arg为表名,然后给+1重新赋值
//然后把新的metrix重新插入到数据库
import java.sql.*;
import com.Myself.database.define.ConnectDatabase;
public class CountMetrix {
//查询出数据库中metrix值并+1
public static int countMetrix(String tableName,int id) throws SQLException{
try{
Connection conn = ConnectDatabase.getConnection();
String sqlQuery = "select metrix from " + tableName+"where id="+id;;
Statement stmt = conn.createStatement();
ResultSet rst = stmt.executeQuery(sqlQuery);
while(rst.next()){
int metrix=rst.getInt(1)+1;
}
}
catch (Exception e){
e.printStackTrace();
}
return metrix;
}
//把新的metrix插入到数据库,要返回是否插入成功
public static int UpdateMetrix(String tableName,int id) throws SQLException{
try{
int isSuccess;
Connection conn = ConnectDatabase.getConnection();
String sqlUpdate = "update "+tableName+"set metrix="+CountMetrix.countMetrix(tableName,id)+"where id="+id;
Statement stmt = conn.createStatement();
isSuccess= stmt.executeUpdate(sqlUpdate);
}
catch (Exception e){
e.printStackTrace();
}
return isSuccess; }
}
上边的红色都会导致出错."cannot resolve symbol",请教应该怎么返回。新手,谢谢您的阅读 |
|