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



Home Answers Viewqa JSP-Servlet inserting into mysql database with uid to be primary key and autoincrement
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Romil Roy
inserting into mysql database with uid to be primary key and autoincrement
1 Answer(s)      2 years and 2 months ago
Posted in : JSP-Servlet


hello, i am new to jsp and facing problem in creating a database entry from a jsp form where there will be a uid which will be auto incremented each time the form will be submitted. pls help me out with any kind of short code example so that i can understand easily. Expecting reply at the earliest . . .


Advertisement
View Answers

July 18, 2012 at 11:26 AM


Here is the code of inserting values from jsp form to database. In the database table, set the uid as the primary key and auto-increment. Then, whenever you submit the form,the values will get inserted along with the uid which is incremented automatically. There is no need to specify it in the query.

1)register.jsp:

<html>
<form method="post" action="insert.jsp">
<table>
<tr><td>First Name:</td><td><input type="text" name="fname"></td></tr>
<tr><td>Last Name:</td><td><input type="text" name="lname"></td></tr>
<tr><td>Email:</td><td><input type="text" name="email"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass"></td></tr>
<tr><td>Confirm Password:</td><td><input type="password" name="cpass"></td></tr>
<tr><td>Date Of Birth</td><td><input type="text" name="dob"></td></tr>
<tr><td>Age:</td><td><input type="text" name="age"></td></tr>
<tr><td>Gender</td><td><input type="text" name="gender"></td></tr>
<tr><td>Address:</td><td><input type="text" name="address"></td></tr>
<tr><td>Country</td><td><input type="text" name="country"></td></tr>
<tr><td>State:</td><td><input type="text" name="state"></td></tr>
<tr><td>City</td><td><input type="text" name="city"></td></tr>
<tr><td>Telephone No:</td><td><input type="text" name="tno"></td></tr>
<tr><td>Mobile:</td><td><input type="text" name="mobile"></td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</form>
</html>

2)insert.jsp:

<%@page import="java.sql.*,java.util.*"%>
<%
String fname=request.getParameter("fname");
String lname=request.getParameter("lname");
String email=request.getParameter("email");
String pass=request.getParameter("pass");
String cpass=request.getParameter("cpass");
String dob=request.getParameter("dob");
int age=Integer.parseInt(request.getParameter("age"));
String gender=request.getParameter("gender");
String address=request.getParameter("address");
String country=request.getParameter("country");
String state=request.getParameter("state");
String city=request.getParameter("city");
int telephone=Integer.parseInt(request.getParameter("tno"));
int mobile=Integer.parseInt(request.getParameter("mobile"));
        try{
         Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/roseindia", "root", "root");
           Statement st=con.createStatement();
           int i=st.executeUpdate("insert into student(firstname,lastname,email,pass,confirm_pass,dob,age,gender,address,country,state,city,telephone,mobile) values('"+fname+"','"+lname+"','"+email+"','"+pass+"','"+cpass+"','"+dob+"',"+age+",'"+gender+"','"+address+"','"+country+"','"+state+"','"+city+"',"+telephone+","+mobile+")");
        out.println("Data is successfully inserted!");
        }
        catch(Exception e){
        System.out.print(e);
        e.printStackTrace();
        }


Related Tutorials/Questions & Answers:
inserting into mysql database with uid to be primary key and autoincrement
inserting into mysql database with uid to be primary key and autoincrement ...; Here is the code of inserting values from jsp form to database. In the database table, set the uid as the primary key and auto-increment. Then, whenever
Mysql Alter Autoincrement
; existing table and add autoincrement feature property to the primary key... 'employees' definition and add autoincrement to the primary key of the table... Mysql Alter Autoincrement      
Advertisements
How to auto increment primary key in mysql...
How to auto increment primary key in mysql...  How to auto increment primary key in mysql... and one more thing i need to get auto increment value in front end each time for inserting the value[JSP
Mysql add primary key
Mysql add primary key       Mysql add Primary Key is used to add primary key on the column... of this tutorial illustrate an example from 'Mysql Add Primary Key'. To understand
Difference between not null and primary key in mysql
Difference between not null and primary key in mysql  Explain not null and primary key in mysql
Mysql Alter Table Primary Key
Mysql Alter Table Primary Key       Mysql Alter Table Primary Key is used to remove the primary key from the table and add primary key to the existing other column of table
Mysql Alter Column Primary Key
Mysql Alter Column Primary Key       Mysql Alter Column Primary Key is used to modify table... The Tutorial illustrate an example from 'Mysql Alter Column Primary Key
primary key and unique key
primary key and unique key  Whats is mean by primary key and unique key
primary key and unique key
primary key and unique key  hello, What's the difference between a primary key and a unique key?   hii, Unique key: column restricts entry of duplicate values but entry of null values is allowed. Primary key
Adding A Primary Key to an Existing Table
a primary key to a MySQL table using Java for two days with no success. I'm new...("Error inserting primary key"); } I get the message: 'error inserting primary key'. Help, please and thank you
Data is not inserting correctly in mysql database.
Data is not inserting correctly in mysql database.  Hello Sir, below is my code to insert data into database by list iteration...( "jdbc:mysql://localhost:3306/asteriskcdrdb", "root", "techsoft
SQL Primary Key
SQL Primary Key       The Primary Key in SQL is used to uniquely identifies each records in a database table. The Primary Key of the Table does not include NULL values
SQL PRIMARY KEY Constraint
SQL PRIMARY KEY Constraint       The PRIMARY KEY constraint is used to uniquely identifies each record in a database table. The Primary keys in a table contains
What is a "primary key"?
What is a "primary key"?  What is a "primary key"?   Hi, Here is the answer, A primary key is used to uniquely identify each row... be an artificial field (one that has nothing to do with the actual record). A primary key can
composinte primary key in hibernate
composinte primary key in hibernate  I am facing following problem, Inside the database(mssql), i have created a table without primary key... primary key with all the columns of the table and hibernate s/w has created two pojo
difference between a primary key and a unique key?
difference between a primary key and a unique key?  What's the difference between a primary key and a unique key?   Hi, The column holding the primary key constraint cannot accept null values.whereas column holding
changing primary key of parent table
changing primary key of parent table  I want to change one primary key from a table where primary key is auto created? How can I do that? I have... to your MySQL server version for the right syntax to use near 'upate person set
SQL PRIMARY KEY Constraint
SQL PRIMARY KEY Constraint       The PRIMARY KEY constraint is used to uniquely identifies each record in a database table. The Primary keys in a table contains a unique
JDBC insert that returns primary key
JDBC insert that returns primary key  How to write code for jdbc insert that returns primary key? Help me fast Thanks   Hi, Following code can be used: //Add record into database String queryInsert = "insert
Create primary key using hibernate
Create primary key using hibernate  How to create primary key using hibernate?   The id element describes the primary key for the persistent class and how the key value is generated.   <id name="id
SQL Alter Table Primary Key
SQL Alter Table Primary Key       Alter a Table Primary Key in SQL modifies the existing table and adds a primary key. Create Table Stu_Table SQL statement to create
hibernate annotations with composite primary key
to use hibernate annotations in case of composite primary key in one table. I tried like this, I have created a table without primary key. By default mssql has created a composite primary key with all the columns of the table. table
how tohow to create a table with primary key and foreign keyhow to create a table with primary key and foreign key
how tohow to create a table with primary key and foreign keyhow to create a table with primary key and foreign key  how to create a table using java language with primary key and foreign key   Hi Friend, A foreign
inserting image in mysql database using browse button in servlets
inserting image in mysql database using browse button in servlets  Hi,I want to insert a image in mysql database using browse button. and, i want... = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); File f = new File
How to create primary key using hibernate?
How to create primary key using hibernate?  Hi, How to create primary key using hibernate? Thanks
php inserting date into mysql
php inserting date into mysql  php inserting date into mysql   Here is a php code that inserts date into mysql database. In the given code, data is a database table. <?php table $query_manual = "INSERT INTO data
query regarding auto increment primary key
query regarding auto increment primary key  i want to use s1,s2.... as my auto increment primary key.cant i do this in mysql
Inserting Data into mysql - SQL
(using php)and inserting the details in a mysql database. Lets take for instance, a csv file named books and a database table named books as well both...Inserting Data into mysql  My sincere apologize for this post. I
Define sequence generated primary key in hibernate
Define sequence generated primary key in hibernate  How to define sequence generated primary key in hibernate?   Use value "sequence" for class attribute for generator tag. <id column="USER_ID" name="id" type
SQL Auto increment Primary Key
SQL Auto increment Primary Key       SQL Auto increment Primary Key is used to allow a unique... increment Primary Key'. To understand this example we use a query 'use
Inserting values in MySQL database table
Inserting values in MySQL database table   ... the facility for inserting the values in MySQL database table. Description... insert values in the MySQL database table. We know that tables store data in rows
 

 

 

DMCA.com