store data from a variable in mysql?

Ads
 

store data from a variable in mysql?

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..?

View Answers

June 25, 2012 at 5:02 PM

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









Related Tutorials/Questions & Answers:
store data from a variable in mysql?
retrieve data from mysql database and store it in a variable ?
Advertisements
Getting Json data from servlet to javascript variable
select maximum from table and hoe to store it in any variable
Store data in HTML forms from Excel sheet
Store Variable in Java
how to store JTree data hierarchically in mysql database from netbeans
I want to store the value of local variable into Global variable
Extracting variable from jsp
declare a variable that will store an array of 5 objects
Store ResultSt Data in a Map
iPad App to store data
Getting environment variable value from java
store and retrive image from the database
store and retrieve data
servlet program for data store in oracle?
Java Xml Data Store
store form data into word document
how to store,retrieve,modify the data
ModuleNotFoundError: No module named 'mysqlx-connector'
data type used to store name.(getting an error)
read from file and store using hash map
store value in checkbox from gridview
store & retrive the image from oracle database
project in JSP and XML(to store data)
unable to get value returned from javascript variable in page
to store data entered in html page
How to store and retrieve image from database in JSP?
Hibernate 5 MySQL5 Dialect
Hibernate 5 MySQL5 Dialect
Hibernate 5 MySQL5 Dialect

Ads