-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAddBookDetials.java
56 lines (47 loc) · 1.62 KB
/
AddBookDetials.java
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
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/addbooks")
public class AddBookDetials extends HttpServlet {
Connection con=null;
@Override
public void init() throws ServletException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/1eja8","root", "sql123");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name=req.getParameter("bookname");
double price=Double.parseDouble(req.getParameter("bookprice"));
String author=req.getParameter("author");
PreparedStatement pstmt=null;
String query="insert into book_data values(?,?,?,?)";
try {
pstmt=con.prepareStatement(query);
pstmt.setInt(1, 0);
pstmt.setString(2, name);
pstmt.setDouble(3, price);
pstmt.setString(4, author);
int count = pstmt.executeUpdate();
PrintWriter pw =resp.getWriter();
pw.print("<h1>"+count+" Recourd Inserted");
} catch (SQLException e) {
e.printStackTrace();
}
}
}