如何在此Java Servlet程序中实现属性文件或属性?以及如何在数据库中存储数据
代码:-公共类InsertData扩展HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
try{
String dbDriver = "com.mysql.jdbc.Driver";
String dbURL = "jdbc:mysql://localhost:3306/";
String dbName = "demo";
String dbUsername = "root";
String dbPassword = "root";
Class.forName(dbDriver);
Connection con = DriverManager.getConnection(dbURL + dbName,
dbUsername,
dbPassword);
PreparedStatement st = con.prepareStatement("insert into details values (?,?,?)");
st.setString(1, request.getParameter("first_name"));
st.setString(2, request.getParameter("last_name"));
st.setString(3, request.getParameter("Address"));
st.executeUpdate();
st.close();
con.close();
}
catch(Exception ex) {
ex.printStackTrace(out);
}
}}}
您可以使用this example。或者,您可以使用this之类的东西。