store data from a variable in mysql?

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+")");


        }
    }
}









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
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
Extracting variable from jsp
declare a variable that will store an array of 5 objects
ModuleNotFoundError: No module named 'mysqlx-connector'
Store ResultSt Data in a Map
iPad App to store data
store and retrieve data
servlet program for data store in oracle?
Java Xml Data Store
Getting environment variable value from java
store form data into word document
PHP MySQLi Affected Rows
store and retrive image from the database
how to store,retrieve,modify the data
Hibernate 5 MySQL5 Dialect
Hibernate 5 MySQL5 Dialect
Hibernate 5 MySQL5 Dialect
data type used to store name.(getting an error)
read from file and store using hash map
project in JSP and XML(to store data)
store value in checkbox from gridview
PHP MySQLi Query
to store data entered in html page
for store data in data base - JSP-Servlet
Getting data from servlet into javascript
ModuleNotFoundError: No module named 'nifeng-data-store'
ModuleNotFoundError: No module named 'wrattler-data-store'
ModuleNotFoundError: No module named 'wrattler-data-store'
unable to get value returned from javascript variable in page
PHP MySQLI Prep Statement
PHP MySQLi
store & retrive the image from oracle database
how to store data in XML file - JSP-Servlet
Assign value from a <bean:write> tag to a variable
read text file and store the data in mysql - JDBC
How to store and retrieve image from database in JSP?
JSP and XML .data store nd retrieve
extract data from HTML
How I get a variable from java script to use it in the scriptlet of jsp.
data science from iim
data analytics from iim
Assigning a value to JSP variable from a bean class. - JSP-Servlet

Ads