Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JDBC select maximum from table and hoe to store it in any variable
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


shiv vaishnav
select maximum from table and hoe to store it in any variable
2 Answer(s)      3 years and 4 months ago
Posted in : JDBC


public class signup extends HttpServlet {

/** 
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String u=request.getParameter("user"); String p=request.getParameter("password"); String c=request.getParameter("city"); String m=request.getParameter("mobile"); String s=request.getParameter("salary");

    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection cn=DriverManager.getConnection("jdbc:odbc:student");
    Statement st=cn.createStatement();
            ResultSet i=st.executeQuery("select MAX(sn) from detail");
            int x=i.getInt(1);
            x=x+1;
            st.executeUpdate("insert into detail values(3,'"+u+"','"+p+"','"+c+"','"+m+"','"+s+"')");
    ResultSet rs=st.executeQuery("select * from detail");    //" & ' me space ni dena h
    while(rs.next()){

            out.println("login success"+"<br>");
                        out.println("ur details are:<br>");

                        out.println("user name : "+rs.getString(2)+"<br>");
                        out.println("city : "+rs.getString(4)+"<br>");
                        out.println("mobile : "+rs.getString(5)+"<br>");
                        out.println("salary : "+rs.getString(6)+"<br>");
            }

    cn.close();
}
catch(ClassNotFoundException e){
    out.print("class not found");
    }
catch(SQLException e){
    out.print("sql error");
    }
         }

}

i want to select max(sn) from table then insert into table as(max+1,user,password,city,mobile,salary).. help me pls


Advertisement
View Answers

July 13, 2011 at 2:52 PM


Try this:

public class signup extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String u=request.getParameter("user"); 
    String p=request.getParameter("password"); 
    String c=request.getParameter("city");
    String m=request.getParameter("mobile");
    String s=request.getParameter("salary");

    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection cn=DriverManager.getConnection("jdbc:odbc:student");
    Statement st=cn.createStatement();
            ResultSet rs=st.executeQuery("select MAX(sn) from detail");
            int x=0;
            while(rs.next()){
             x=rs.getInt(1);
            }
            int y=x+1;
            st.executeUpdate("insert into detail values("+y+",'"+u+"','"+p+"','"+c+"','"+m+"','"+s+"')");
            ResultSet rst=st.executeQuery("select * from detail");    
            if(rst.last()){
            out.println("login success"+"<br>");
                        out.println("ur details are:<br>");
                        out.println("user name : "+rst.getString(2)+"<br>");
                        out.println("city : "+rst.getString(4)+"<br>");
                        out.println("mobile : "+rst.getString(5)+"<br>");
                        out.println("salary : "+rst.getString(6)+"<br>");
            }

     cn.close();
    }
    catch(ClassNotFoundException e){
    out.print("class not found");
    }
    catch(SQLException e){
    out.print("sql error");
    }
         }

}


July 13, 2011 at 2:52 PM


Try this:

public class signup extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String u=request.getParameter("user"); 
    String p=request.getParameter("password"); 
    String c=request.getParameter("city");
    String m=request.getParameter("mobile");
    String s=request.getParameter("salary");

    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection cn=DriverManager.getConnection("jdbc:odbc:student");
    Statement st=cn.createStatement();
            ResultSet rs=st.executeQuery("select MAX(sn) from detail");
            int x=0;
            while(rs.next()){
             x=rs.getInt(1);
            }
            int y=x+1;
            st.executeUpdate("insert into detail values("+y+",'"+u+"','"+p+"','"+c+"','"+m+"','"+s+"')");
            ResultSet rst=st.executeQuery("select * from detail");    
            if(rst.last()){
            out.println("login success"+"<br>");
                        out.println("ur details are:<br>");
                        out.println("user name : "+rst.getString(2)+"<br>");
                        out.println("city : "+rst.getString(4)+"<br>");
                        out.println("mobile : "+rst.getString(5)+"<br>");
                        out.println("salary : "+rst.getString(6)+"<br>");
            }

     cn.close();
    }
    catch(ClassNotFoundException e){
    out.print("class not found");
    }
    catch(SQLException e){
    out.print("sql error");
    }
         }

}


Related Tutorials/Questions & Answers:
select maximum from table and hoe to store it in any variable
select maximum from table and hoe to store it in any variable  public..."); } } } i want to select max(sn) from table then insert into table...+"','"+s+"')"); ResultSet rs=st.executeQuery("select * from detail"); //" &
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... of the calculation from an int variable into mysql in a new table of database. how
Advertisements
Hoe to refresh a table row dynamically
Hoe to refresh a table row dynamically  Want to refresh a table data... Friend, Is there a link attribute with every row of a table through which we will able to refresh a particular table row? Please clarify this. Thanks
retrieve data from mysql database and store it in a variable ?
retrieve data from mysql database and store it in a variable ?  sir... to store that data in int variable. how to do this ?   Here is an example...=conn.createStatement(); ResultSet rs=st.executeQuery("select * from numbers
mysql select into table from another table example
mysql select into table from another table example  Can you suggest the correct example of select into table from another table example in MySQL... from one table into another table. Check the example at MySQLselect into new
Table maximum Row count - JDBC
Table maximum Row count  Dear friends I want to select maximum time entry Status from db. for example : Status : 3 4 6 8 3 5 7 5..., Sql Query for select maximum value max() function use. "select max
Select data from Table in Database
Select data from Table in Database   ... the selected data from table in database. The select query in the database retrieve...; in simple select statement is given below select column name from table
select maximum time entry row - JDBC
select maximum time entry row  Hi fried you given ans this Sql Query for select maximum value max() function use. "select max(fieldname... is maximum time entry in column Status please if you have any other query
Store Variable in Java
Store Variable in Java  How to store Variables in Java?   public class DoubleTest { public static void main(String[] args) { double...(aDouble); // Outputs 7.21 } }   Variable in Java
SQL Select From
SQL Select From       SQL Select From keyword show you the records from a specified table. Understand... keyword in SQL is used to show the records from table 'Stu'. select
What?s the maximum size of a row in SQL table?
What?s the maximum size of a row in SQL table?  What?s the maximum size of a row in SQL table?   Hi, The maximum Row Size is 8060 Bytes in a sql table. Thanks
I want to store the value of local variable into Global variable
I want to store the value of local variable into Global variable    <%=cnt%>=x; is it a valid Statement
Select from select list + display
Select from select list + display  i have a select list containing... select EmpCode from the select list, the corresponding EmpName and DeptName should...=con.createStatement(); ResultSet rs=st.executeQuery("select * from emp
How to display all the Select values from the MySQL database table in where condition= In JSP?
How to display all the Select values from the MySQL database table in where... to display all the select values from MySQL DB** only first value is displayed in the jsp file. @select * from table dept where dept_no=10;" jsp code i have used
declare a variable that will store an array of 5 objects
declare a variable that will store an array of 5 objects  Q7. Give a declaration for a variable that will 'store' an array of 5 Card objects. this is my segment: list = new int[5]; I don't know if it is right or wrong. So
SQL select statement doesn't return any rows,
SQL select statement doesn't return any rows,  When an SQL select statement doesn't return any rows, is an SQLException thrown
how I do select from select in hql
how I do select from select in hql  select RN from ( select rownum RN, dbid from ( select * from profile p where type = 1912 and name like... doesn't work select RN from ( select rownum RN, DBId from ( select distinct p
Extracting variable from jsp
Extracting variable from jsp  how to Write a java program which will be extracting the variables and putting them in an excel format?   The given code allow the user to enter some fields and using the POI API
retrieve value from database on the basis of maximum id number
retrieve value from database on the basis of maximum id number  hi, i want to retrieve value of maximum id number from the database and show that value in jTextField.when the user clicks on the button that maximum id number
How many cookie object we can store in a session? is there any limit?
How many cookie object we can store in a session? is there any limit?  How many cookie object we can store in a session? is there any limit
store and retrive image from the database
store and retrive image from the database  please provide me... to store and retreive images from sql database using sql commands -how to store and retreive images from sql database using asp.net/c# thanks in advance
store value in checkbox from gridview
store value in checkbox from gridview  Please check the error... = con.createStatement(); rst=st.executeQuery("select * from books...; <table bgcolor="pink" border="1" cellspacing="0" cellpadding="0">
Using Select Statements in JDBC
; The SELECT statement is used to select data from a table. For terminating a statement, semicolon is used. When we want to fetch the data from any table... to retrieve the data from any table with the JDBC. Employee is the name of a table
Select data from diplicated data
Select data from diplicated data  hello i have table location...++) { $SQL = "SELECT duration FROM core_network WHERE location=('".$location_c[$k... = mysql_select_db($database, $db_handle); if ($db_found) { $SQL = "SELECT
mysql select from multiple tables
mysql select from multiple tables   how can i select the date field from multiple tables in mysql?   "UNION" can be used to select data from multiple tables in my sql... for example (SELECT * from name where `name
Extracting equation from any document
Extracting equation from any document  how can i extract equation from any word document?   Hello Friend, If you want to extract data from the word file, then visit the following link: Extract data from word document
update one table from another table
update one table from another table  hello, i need to update one.... for example TotalQuantity field from Products table should be updated if i... * from (select sum(products.totalquantity)+sum(productspurchased.quantity) from
MySQL Select Statement
clause. The SELECT statement is used to retrieve the records from the table... of Emp table. mysql> SELECT * FROM Emp; If you want... fields from the Emp table. mysql> SELECT Name, City, Age from EMP
read from file and store using hash map
read from file and store using hash map  I was stuck with a java project where I have to read a file with two different concepts and store them... different maps. Any help will be appreciated. Thanks in advance
Getting environment variable value from java
Getting environment variable value from java  Getting environment variable value from java
send multiple textbox vaues in to an jsp form to store them in a DB table
values in to an jsp file with additional values to store those values in to an database table. Please help me...... looking forward to hear from you...send multiple textbox vaues in to an jsp form to store them in a DB table 
Advertisements
 

 

 

DMCA.com