Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

netbeans 8 - Why created table in SQL statement from Servlet does not show up in DataBase

Project is named DBZ, i created 2 packages first called "data" and second is "servleti". Data package contains one database called DB

   package data;


 import java.sql.Connection;
 import java.sql.DriverManager;
 public class DB {

 private static DB instance;
 private String kon = "jdbc:derby://localhost:1527/DBZ"; 

 private Connection con;


 private DB() { 

    try {
        Class.forName("org.apache.derby.jdbc.ClientDriver");
        con = DriverManager.getConnection(kon, "root", " ");
    } catch (Exception e) {
        e.printStackTrace();
    }
  }

 public static DB getInstance() {
    if (instance == null) {
        instance = new DB();
    }
        return instance;
  }

 public synchronized Connection getConnection() {
    return con;
 }

 }

package "servleti" contain one servlet called "DBServlet" where i created table using sql:

  package servleti;


 import java.io.IOException;
 import java.sql.Connection;
 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;

 import data.DB;


 public class DBServlet extends HttpServlet {


  protected void processRequest(HttpServletRequest request, 
   HttpServletResponse response) throws ServletException, IOException {
    
    Statement stmt = null;
    Connection con = null;

    try {
      con = DB.getInstance().getConnection();
      if (con == null) return;
  

      stmt = con.createStatement();
      String query = "CREATE TABLE APP.STUDENTI ("
              + "ID_S  INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS 
      IDENTITY (START WITH 1, INCREMENT BY 1), "
              + "PREZIME VARCHAR(30) NOT NULL,"
              + "IME VARCHAR(30) NOT NULL,"
              + "BROJINDEKSA INTEGER NOT NULL,"                  
              + "GODINAINDEKSA INTEGER NOT NULL, SMER INTEGER NOT NULL)";

      stmt.executeUpdate(query);

      query = "INSERT INTO APP.STUDENTI (PREZIME, IME, BROJINDEKSA, 
     GODINAINDEKSA, SMER) VALUES ";
      stmt.executeUpdate(query + "('Petrovic', 'Ana', 307, 17, 1)");
      stmt.executeUpdate(query + "('Nenadovic', 'Milan', 97, 16, 2)");
      stmt.executeUpdate(query + "('Nesic', 'Nadica', 34, 18, 3)");
      stmt.executeUpdate(query + "('Markovic', 'Marko', 55, 19, 3)");
      stmt.executeUpdate(query + "('Petkovic', 'Zoran', 109, 19, 3)");
      stmt.executeUpdate(query + "('Savic', 'Nenad', 12, 18, 1)");    
      stmt.executeUpdate(query + "('Aleksic', 'Milica', 34, 15, 2)");    
      stmt.executeUpdate(query + "('Ivanovic', 'Ana', 37, 19, 2)");              
      stmt.close();

      response.sendRedirect("potvrda.html"); //
      
    } catch (SQLException e) {
        response.sendRedirect("greska.html");
    }
  
   }

    @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException {
    processRequest(request, response);
}

 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse 
 response) throws ServletException, IOException {
    processRequest(request, response);
 }


 }

after that i writed my index.html and added to Libraries Java DB Driver

 <!DOCTYPE html>

 <html>
 <head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
 </head>
 <body>
    <a href="DBServlet">Create</a>
 </body>
 </html>

THEN I CREATED DataBase IN SERVICES called DBZ username = root

(jdbc:derby://localhost:1527/DBZ [root on APP]) saved all run program and still don't have table created

xml: glassfish

   <?xml version="1.0" encoding="UTF-8"?>
 <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
   http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
 <servlet>
    <servlet-name>DBservlet</servlet-name>
    <servlet-class>servleti.DBservlet</servlet-class>
 </servlet>
 <servlet-mapping>
    <servlet-name>DBservlet</servlet-name>
    <url-pattern>/DBservlet</url-pattern>
 </servlet-mapping>
 <session-config>
    <session-timeout>
        30
    </session-timeout>
     </session-config>
     </web-app>
question from:https://stackoverflow.com/questions/65905249/why-created-table-in-sql-statement-from-servlet-does-not-show-up-in-database

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...