sir last time asked you tell me how to retrieve data from a database mysql and store it in an int variable in order to apply some calculation on it, but now i want to store the result of the calculation from an int variable into mysql in a new table of database. how to do this..?
Here is an example that retrieve the integer values from the database and stored in the integer variables. In addition to it, we have added numbers that are retrieved from the database. The sum of numbers is then saved to another table.
import java.sql.*; class RetrieveData { public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root" ); Statement st=conn.createStatement(); ResultSet rs=st.executeQuery("select * from numbers"); Statement stmt=conn.createStatement(); while(rs.next()){ int num1=rs.getInt("number1"); int num2=rs.getInt("number2"); int sum=num1+num2; System.out.println(num1+" +\t "+num2+" =\t "+sum); int i=stmt.executeUpdate("insert into result(result) values("+sum+")"); } } }
Ads