Share on Google+Share on Google+

srikanthkumar
dbms
1 Answer(s)      6 years and 2 months ago
Posted in : Java Magazine

how to sum 2 columns in a table and put the result in new coluumn. the o/p should be

col1 col2 col3 12 24 36 10 30 40 5 10 15

Ads

View Answers

March 1, 2011 at 1:39 PM


JDBC sum two columns and display the result in third column

import java.sql.*;
public class DatabaseExample
{
    public static void main(String[]args){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement st=con.createStatement();
Statement st1=con.createStatement();
ResultSet rs=st.executeQuery("select * from sum");
while(rs.next()){
    int id=rs.getInt("id");
int sum=rs.getInt("col1")+rs.getInt("col2");
st1.executeUpdate("update sum set col3="+sum+" where id="+id+"");
}
Statement stmt=con.createStatement();
ResultSet rs1=stmt.executeQuery("select * from sum");
while(rs1.next()){
    System.out.println(rs1.getInt("col1")+"\t "+rs1.getInt("col2")+"\t "+rs1.getInt("col3"));
}
}
catch(Exception e){
System.out.println(e);
}
    }
}









Related Tutorials/Questions & Answers:
Advertisements
Isolation level is used by the DBMS.
Isolation level is used by the DBMS.  What isolation level is used by the DBMS when inserting, updating and selecting rows from a database
Advanced DBMS questions
Advanced DBMS questions   Five company/organization that involves in SQL Server database migration. what is the Transaction Recovery Method. What does Cloud Computing contributes in the RDBMS? Importance
the isolation levels supported by my DBMS
the isolation levels supported by my DBMS  How can I determine the isolation levels supported by my DBMS
Ask DBMS Questions online
Ask DBMS Questions online       DBMS stands for Data Base Management System (DBMS) is a set of computer programs that controls the creation, organization
dbms_change_notification new_reg_start
dbms_change_notification new_reg_start  Please explain it "dbmschangenotification newregstart" Thanks in advance
use JDBC to execute non-standard features that my DBMS provides.
use JDBC to execute non-standard features that my DBMS provides.  Can I use JDBC to execute non-standard features that my DBMS provides
Database Management System (DBMS)
Database Management System (DBMS) A Database Management System (DBMS) sometimes called a database manager or database system is a set... of the data. A DBMS is a system software package that ensures the integrity
Open Source DBMS
Open Source DBMS Open Source Database Management Systems The rapid... of its adopters.    Open source DBMS vendors Like other... evaluating several database software vendors, Holden chose MySQL DBMS (database
Database Connectivity Example In Java
that converts Java program requests to a protocol into which the DBMS can... will try to create a connection with the DBMS and then we will write the SQL... DBMS to make the connection between Java program and DBMS to access database
pls answer - Security
pls answer  what are the difference of open source dbms and proprietary dbms
DIFFFERENCE
DIFFFERENCE  WHAT IS THE DIFF B/W DBMS AND RDBMS
SQL Server
SQL Server  Hii, What is SQL Server ?   hello, SQL Server is a DBMS system provided by Microsoft
A connection pool with multiple user ID.
A connection pool with multiple user ID.  Can I set up a connection pool with multiple user IDs? The single ID we are forced to use causes problems when debugging the DBMS
Class.forName in jsp
a connection with a DBMS
Collection
Collection  actually why do we need collections? means we can also store group of objects in relational data base and dbms also provides all the operatoins insert,delete,update,sort,search etc. then why collection
Java - JDBC
databases.  Difference between DBMS and RDBMS A DBMS is persistent... to exist or even the application that created the data restarted. A DBMS also... relationships between tables. DBMS does not impose any constraints or security
What is a database?
System (DBMS). 2 Relational Database Management System (RDBMS). 1. DBMS : DBMS stands for database management system and it includes theoretical part... can access the database at the given time in DBMS.   In DBMS, you can
jdbc interview question
. It translates JDBC calls to a DBMS-independent network protocol, which is then translated to a DBMS protocol by a server. Flexible. Pure Java and no native code. Type 4... machine to the DBMS server. It doesn?t need any special native code on the client
Tutorials   
Java Spring Hibernate Struts Training Default value of path variable in Windows 7 Windows 7 refresh DNS 550 Access denied - Invalid HELO name (See RFC2821 4.1.1.1) how to change a column data in a every row in a text file in java cursor location what is the Advantages moqui framework vs other frameworks what is the difference between moqui framework vs other frameworks Develop Bulk SMS sender Bundle of servers interface ALU Computer Term How to find the size of a dynamic webpage using java... eg : Youtube........ Question in Create Arraylist ? active window means 1024 bytes equals to can a volatile memory loss data on power cut What is E-Learning? What is E-Learning? What is hacker? different types of storage devices of a computer Examples of optical disk Main job of cpu Main memory is also known as Define computer software. connection between your computer and local isp which part of the cpu does math calculations What is procedure Where i can use UNIX Operating System? What was ENIAC One byte consists of how many bits? What is the use of Bluetooth technology basic function of computer system how to measure data transfer speed GIGO Full Form What is parallel port where are program instructions and data values stored Nible is a computer network is referred as The input device used by computer is The product of data processing is The CPU(Central processing unit)consist of One-thousand of a second is called Alternate facilities of programs, data files, hardware equipment, etc. One-Millionth of a second is referred as Storage medium that loses its contents A LSI chip contains the entire CPU of computer except main memory is called ascending order of a data hierarchy Combining records from two or more orders files into a single ordered file is called Computer questions for Bank PO Exams and upsc competitive exams characteristics of a modem computer

Ads

 
Advertisement null

Ads