wap in java to insert a record from a form to table

Solutions on MaxInterview for wap in java to insert a record from a form to table by the best coders in the world

showing results for - "wap in java to insert a record from a form to table "
Erika
06 Sep 2017
1import java.io.DataInputStream;
2import java.sql.Connection;
3import java.sql.DriverManager;
4import java.sql.PreparedStatement;
5import java.sql.Statement;
6
7public class ExPrepareStatement {
8	public static void main(String[] args) {
9		try{
10			Class.forName("com.mysql.jdbc.Driver").newInstance();
11
12			//serverhost = localhost, port=3306, username=root, password=123
13			Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","123");
14
15			DataInputStream KB=new DataInputStream(System.in);
16
17			//input employee id
18			System.out.print("Enter Employee ID: ");
19			String eid=KB.readLine();
20			//input employee name
21			System.out.print("Enter Employee Name: ");
22			String en=KB.readLine();
23			//input employee Date Of Birth
24			System.out.print("Enter Employee Date Of Birth: ");
25			String ed=KB.readLine();
26			//input employee city
27			System.out.print("Enter Employee City: ");
28			String ec=KB.readLine();
29			//input employee Salary
30			System.out.print("Enter Employee Salary: ");
31			String es=KB.readLine();
32
33			//creating object of PreparedStatement class and passing parameter (?)
34			PreparedStatement smt=cn.prepareStatement("insert into employees values(?,?,?,?,?)");
35
36			// set the values
37			smt.setString(1, eid);
38			smt.setString(2, en);
39			smt.setString(3, ed);
40			smt.setString(4, ec);
41			smt.setInt(5, Integer.parseInt(es));
42
43			//to execute update
44			smt.executeUpdate();
45			System.out.println("Record Submitted....");
46			
47			//close the file
48			cn.close();
49		}
50		catch(Exception e){
51			System.out.println(e);
52		}
53	}
54}
55