forked from kundanRJ/Virtual-Class
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackendProcess
More file actions
83 lines (67 loc) · 2.35 KB
/
BackendProcess
File metadata and controls
83 lines (67 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.anjani.servlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
@WebServlet("/ServletEntryPoint")
public class ServletEntryPoint extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/mydatabase";
private static final String DB_USER = "username";
private static final String DB_PASSWORD = "password";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
System.out.println("Enter in post section ");
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String email = request.getParameter("email");
String mobile = request.getParameter("mobile");
String message = request.getParameter("message");
Connection conn = null;
PreparedStatement stmt = null;
try {
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
conn = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PASSWORD);
// Execute SQL query
String sql = "INSERT INTO contact_info (first_name, last_name, email, mobile, message) VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, firstName);
stmt.setString(2, lastName);
stmt.setString(3, email);
stmt.setString(4, mobile);
stmt.setString(5, message);
stmt.executeUpdate();
out.println("<html><body>");
out.println("<h2>Record inserted successfully!</h2>");
out.println("</body></html>");
} catch (SQLException se) {
se.printStackTrace();
out.println("SQLException: " + se.getMessage());
} catch (Exception e) {
e.printStackTrace();
out.println("Exception: " + e.getMessage());
} finally {
// Clean-up environment
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
se2.printStackTrace();
}
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
out.close();
}
}
}