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
Store data in HTML forms from Excel sheet
select maximum from table and hoe to store it in any variable
Store Variable in Java
how to store JTree data hierarchically in mysql database from netbeans
how to store the data in a array retrived from the database - JSP-Servlet
I want to store the value of local variable into Global variable
Store ResultSt Data in a Map
iPad App to store data
declare a variable that will store an array of 5 objects
store and retrieve data
Java Xml Data Store
Extracting variable from jsp
servlet program for data store in oracle?
store form data into word document
how to store,retrieve,modify the data
data type used to store name.(getting an error)
project in JSP and XML(to store data)
PHP MySQLi Affected Rows
for store data in data base - JSP-Servlet
store and retrive image from the database
to store data entered in html page
Getting environment variable value from java
how to store data in XML file - JSP-Servlet
read text file and store the data in mysql - JDBC
Getting data from servlet into javascript
JSP and XML .data store nd retrieve
PHP MySQLi Query
read from file and store using hash map

Ads