Di sini kami memilih nama proyek sebagai "DBMySQLEclipse" => Next => Next => Pilih opsi untuk menghasilkan web. deskriptor penerapan xml
Langkah 2) Tambahkan file jar konektor MySQL ke folder lib aplikasi web
This is the most important part of this example. Download the latest suitable JDBC driver of the MySQL database. Open the downloaded zip file and extract it. The mysql-connector-java-.jar is the main Jar file.
Untuk menambahkan file jar itu, cukup salin (pilih file jar, CTRL + C) dan tempel (CTRL + V) ke folder lib proyek web Eclipse. Folder lib terletak di DBMySQLEclipse\src\main\webapp\WEB-INF\lib
[email protected]"); // compile SQL query and store it in // PreparedStatement object if (con != null) ps = con.prepareStatement(SELECT_STUDENT_QUERY); // set input value to query parameter if (ps != null) ps.setInt(1, sno); // execute the query if (ps != null) rs = ps.executeQuery(); // process the result if (rs != null) { while (rs.next()) { // display result flag = true; pw.println("<h1>Student Details, </h1>" + "Name: " + rs.getString("SNAME") + "<br>" + "Address: " + rs.getString("SADD") + "<br>" + "Average: " + rs.getDouble("AVG") + "<br>"); } } // Student not found if (!flag) { pw.println("<h1>Student Not Found.</h1>"); } } catch (SQLException se) { se.printStackTrace(); pw.println("Error Occured"); } catch (Exception e) { e.printStackTrace(); pw.println("Unknown Exception Occured"); } finally { // close JDBC connection try { if (rs != null) rs.close(); } catch (SQLException se) { se.printStackTrace(); } try { if (ps != null) ps.close(); } catch (SQLException se) { se.printStackTrace(); } try { if (con != null) con.close(); } catch (SQLException se) { se.printStackTrace(); } // Link to home pw.println("<h3><a href='input.html'>Home</a></h3>"); // close stream pw.close(); } } @Override public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doGet(req, res); } } _
Since it was a very basic and simple web application, therefore we are hardcoding the technical input values like JDBC driver class name, URL, database username, and password. But it is always recommended to gather those values from the web.xml file using or through ServletConfig/ServletContext values.
Deskriptor penerapan (web. xml),
Lihat kode sumber untuk aplikasi web ini (untuk menghubungkan Servlet ke Database MySQL di Eclipse) di GitHub