Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package servlets;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class DodajOsoby extends HttpServlet {
- private static final long serialVersionUID = 1L;
- private final String dbURL = "jdbc:derby://localhost:1527/nazwabazy;create=true;user=test;password=test";
- private final String tableName = "osoby";
- // jdbc Connection
- private Connection conn = null;
- private Statement stmt = null;
- @Override
- public void init() throws ServletException {
- super.init();
- connectDB();
- createTable();
- shutdown();
- }
- private void shutdown() {
- try {
- if (stmt != null) {
- stmt.close();
- }
- if (conn != null) {
- DriverManager.getConnection(dbURL + ";shutdown=true");
- conn.close();
- }
- } catch (SQLException sqlExcept) {}
- }
- private void connectDB() {
- try {
- Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
- //Get a connection
- conn = DriverManager.getConnection(dbURL);
- } catch (Exception except) {
- except.printStackTrace();
- }
- }
- private void createTable() {
- try {
- stmt = conn.createStatement();
- stmt.execute("create table "+ tableName +"(id int primary key, imie varchar(20), nazwisko varchar(30) )");
- stmt.close();
- } catch (SQLException sqlExcept) { }
- }
- private void insertOsoba(int id, String imie, String nazwisko) {
- try {
- stmt = conn.createStatement();
- stmt.execute("insert into " + tableName + " values (" +
- id + ",'" + imie + "','" + nazwisko +"')");
- stmt.close();
- } catch (SQLException sqlExcept) { }
- }
- private void printData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("text/html; charset=UTF-8");
- PrintWriter out = response.getWriter();
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Pomyslnie dodano!</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<h1>Dodano!!!</h1>");
- out.println("<tr>");
- connectDB();
- int id3=0;
- try {
- stmt = conn.createStatement();
- String firstname = request.getParameter("firstname");
- String lastname = request.getParameter("lastname");
- ResultSet results = stmt.executeQuery("select id from " + tableName);
- while(results.next()) {
- id3 = results.getInt(1);
- id3=id3+1;
- insertOsoba(id3, firstname, lastname);
- shutdown();}
- stmt.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- shutdown();
- out.println("</tr>");
- out.println("</table>");
- out.println("</body>");
- out.println("</html>");
- }
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- printData(request, response);
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- printData(request, response);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement