调用servlet的函数,该servlet创建具有互连关系的3个表

时间:2014-07-27 15:30:47

标签: java sql-server servlets

如何创建调用servlet的功能,该servlet创建3个具有互连关系的表,这意味着有一个外键?这些是代码。

CreateTable.java

package com.dls.csb.process.web.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dls.csb.utility.SQLOperations;

@WebServlet("/createtable.html")
public class CreateTable extends HttpServlet {
private static final long serialVersionUID = 1L;


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


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException {
    try {
        SQLOperations.createTable();    


        response.sendRedirect("index.jsp");

    }
    catch(Exception e) {
        response.sendRedirect("errordisplay.jsp?code=2");
    }

}
}

SQLOperations.java

package com.dls.csb.utility;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import javax.sql.*;
import javax.naming.*;
import com.ats.model.Account;


public class SQLOperations implements SQLCommands{

public static void createTable() throws SQLException {
        try {
            PreparedStatement pstmt = 
                    getConnection().prepareStatement(CREATE_ACCOUNT_TABLE);

            pstmt.executeUpdate();              
        }
        catch(SQLException sqle){
            throw new SQLException();
        }
    }

  public static void createTable2() throws SQLException {
        try {
            PreparedStatement pstmt = 
                    getConnection().prepareStatement(CREATE_STATUS_TABLE);

            pstmt.executeUpdate();              
        }
        catch(SQLException sqle){
            throw new SQLException();
        }
    }
}

SQLCommands.java

package com.dls.csb.utility;

public interface SQLCommands {

String DS_SOURCE = "java:comp/env/jdbc/ATS";
String CONNECTION_NOT_FOUND = "Invalid connection. Unable to process request.";

//CRUD statements   

String CREATE_ACCOUNT_TABLE="create table Account("+
         "id int IDENTITY (1,1), "+
         "username varchar(50), "+
         "password varchar(50), "+
         "lastname varchar(50), "+
         "firstname varchar(50), "+
         "middlename varchar(50), "+
         "nationality varchar(50), "+
         "gender varchar(50), "+
         "country varchar(50), "+
         "email varchar(50), "+
         "resume varbinary(MAX), "+
         "filename varchar(MAX)" +
         "statusID int,"+
         " CONSTRAINT PK_id PRIMARY KEY (id))";

String CREATE_STATUS_TABLE ="create table Status(" +
         "statusID int IDENTITY(1,1), "+
         "status varchar(50), "+
         "PRIMARY KEY(statusID),"+
         "FOREIGN KEY (id) REFERENCES ACCOUNT(id))"              
        ;

0 个答案:

没有答案