Jump to content

Executing Java Code via Browser


Guest NHLRetro

Recommended Posts

Guest NHLRetro

I'm coding in Netbeans 5.5 and I wrote a little Java program that parses a text file and sends the information to a local SQL database. Unfortunately (I think), since I wrote it in Java, I cannot upload this file to my web server, run the code again, and have it write to a SQL server that is on the server. Is there a way to write a JSP page or something to execute this code so I can just go to the JSP (or what have you) page on my server, have it run the code, and nicely view the results on the SQL web server? NB, this code works when it writes to my local server, but when I attempt to change the driver and URL to have it point to my server's SQL, my web host has disabled outside connections... so I tried that alley to no avail. I'll attach my Java code (stuff in brackets is correct on my side, but I didn't include it for the Internet to see).

package [PACKAGENAME];import java.io.*;import java.sql.*;public class Players {    static final String JDBC_Driver = "[DRIVER]";    static final String DATABASE_URL = "[url]";    public static void main(String[] args) throws IOException {        Connection connection = null;        Statement statement = null;        try {            Class.forName(JDBC_Driver);            connection = DriverManager.getConnection(DATABASE_URL,"[uSERNAME]","[PASSWORD]");            statement = connection.createStatement();            FileReader file = new FileReader("players.txt");            BufferedReader fileInput = new BufferedReader(file);            String text;            String[] data;            int p_id = Integer.parseInt(fileInput.readLine().trim()) + 1;            final int NUMBER_OF_LINES_IN_FILE = 20;            for(int idindex=1;idindex < p_id;idindex++) {                statement.executeUpdate("insert into \"PLAYERS\" (\"P_ID\") values (" + idindex + ")");                for(int counter=0;counter < NUMBER_OF_LINES_IN_FILE;counter++) {                    text = fileInput.readLine();                    if (counter==0) {                        text = text.trim();                        data = text.split("  ");                        statement.executeUpdate("update \"PLAYERS\" set \"P_SHOOTING\" = " + Integer.parseInt(data[0]) + ", \"P_PLAYMAKING\" = " + Integer.parseInt(data[1]) + ", \"P_HANDLING\" = " + Integer.parseInt(data[2]) + ", \"P_CHECKING\" = " + Integer.parseInt(data[3]) + ", \"P_MARKING\" = " + Integer.parseInt(data[4]) + ", \"P_HITTING\" = " + Integer.parseInt(data[5]) + ", \"P_SKATING\" = " + Integer.parseInt(data[6]) + ", \"P_ENDURANCE\" = " + Integer.parseInt(data[7]) + ", \"P_PENALTY\" = " + Integer.parseInt(data[8]) + ", \"P_FACEOFFS\" = " + Integer.parseInt(data[9]) + " where \"P_ID\" = " + idindex);                    }                    if (counter==1) {                        text = text.trim();                        data = text.split("  ");                        statement.executeUpdate("update \"PLAYERS\" set \"P_LEADERSHIP\" = " + Integer.parseInt(data[0]) + ", \"P_STRENGTH\" = " + Integer.parseInt(data[1]) + ", \"P_POT\" = " + Integer.parseInt(data[2]) + ", \"P_CON\" = " + Integer.parseInt(data[3]) + ", \"P_GREED\" = " + Integer.parseInt(data[4]) + ", \"P_FIGHTING\" = " + Integer.parseInt(data[5]) + ", \"P_CLICK\" = " + Integer.parseInt(data[6]) + ", \"P_TEAM\" = " + Integer.parseInt(data[7]) + ", \"P_MPOS\" = " + Integer.parseInt(data[8]) + ", \"P_COUNTRY\" = " + Integer.parseInt(data[9]) + ", \"P_HANDED\" = " + Integer.parseInt(data[10]) + " where \"P_ID\" = " + idindex);                    }                    if (counter==2) {                        text = text.trim();                        data = text.split("  ");                        if (Integer.parseInt(data[0]) == 0) {                            statement.executeUpdate("delete from \"PLAYERS\" where \"P_ID\" = " + idindex);                            fileInput.close();                            statement.close();                            connection.close();                            System.exit(0);                        }                        statement.executeUpdate("update \"PLAYERS\" set \"P_BIRTH\" = \'" + data[0] + "-" + data[2] + "-" + data[1] + "\', \"P_SALARY\" = " + Integer.parseInt(data[3]) + ", \"P_LENGTH\" = " + Integer.parseInt(data[4]) + ", \"P_DRAFTY\" = " + Integer.parseInt(data[5]) + ", \"P_DRAFTR\" = " + Integer.parseInt(data[6]) + ", \"P_DRAFTT\" = " + Integer.parseInt(data[7]) + ", \"P_RIGHTS\" = " + Integer.parseInt(data[8]) + " where \"P_ID\" = " + idindex);                    }                    if (counter==3) {                        text = text.trim();                        data = text.split("  ");                        statement.executeUpdate("update \"PLAYERS\" set \"P_MOGM\" = " + Integer.parseInt(data[0]) + ", \"P_MOGO\" = " + Integer.parseInt(data[1]) + ", \"P_MOAS\" = " + Integer.parseInt(data[2]) + ", \"P_MOGW\" = " + Integer.parseInt(data[3]) + " where \"P_ID\" = " + idindex);                    }                    if (counter==4) {                        text = text.trim();                        data = text.split("  ");                        statement.executeUpdate("update \"PLAYERS\" set \"P_WKGM\" = " + Integer.parseInt(data[0]) + ", \"P_WKGO\" = " + Integer.parseInt(data[1]) + ", \"P_WKAS\" = " + Integer.parseInt(data[2]) + ", \"P_WKGW\" = " + Integer.parseInt(data[3]) + " where \"P_ID\" = " + idindex);                    }                    if (counter==5) {                        text = text.trim();                        data = text.split("  ");                        statement.executeUpdate("update \"PLAYERS\" set \"P_RECGO\" = " + Integer.parseInt(data[0]) + ", \"P_RECAS\" = " + Integer.parseInt(data[1]) + ", \"P_RECPT\" = " + Integer.parseInt(data[2]) + ", \"P_NOTRADE\" = " + Integer.parseInt(data[3]) + ", \"P_TWOWAY\" = " + Integer.parseInt(data[4]) + ", \"P_OPTION\" = " + Integer.parseInt(data[5]) + " where \"P_ID\" = " + idindex);                    }                    if (counter==6) {                        text = text.trim();                        data = text.split("  ");                        statement.executeUpdate("update \"PLAYERS\" set \"P_STATUS\" = " + Integer.parseInt(data[0]) + ", \"P_ROOKIE\" = " + Integer.parseInt(data[1]) + ", \"P_INJURY\" = " + Integer.parseInt(data[5]) + " where \"P_ID\" = " + idindex);                    }                    if (counter==10) {                        text = text.trim();                        if (text.matches("(\\p{Pd})(\\d+)(.+)(\\p{Pd})(\\d+)(.+)")) {                            data = text.split("  ");                            data[0] = text.substring(0,3).trim();                            statement.executeUpdate("update \"PLAYERS\" set \"P_GSTREAK\" = " + Integer.parseInt(data[0]) + " where \"P_ID\" = " + idindex);                            data[0] = text.substring(3,6).trim();                            statement.executeUpdate("update \"PLAYERS\" set \"P_PSTREAK\" = " + Integer.parseInt(data[0]) + ", \"P_TOTALGP\" = " + Integer.parseInt(data[1]) + ", \"P_SUSPENDED\" = " + Integer.parseInt(data[2]) + ", \"P_TRAINING\" = " + Integer.parseInt(data[3]) + ", \"P_WEIGHT\" = " + Integer.parseInt(data[4]) + ", \"P_HEIGHT\" = " + Integer.parseInt(data[5]) + ", \"P_ORGSTATUS\" = " + Integer.parseInt(data[6]) + " where \"P_ID\" = " + idindex);                        }                        else {                            data = text.split("  ");                            statement.executeUpdate("update \"PLAYERS\" set \"P_GSTREAK\" = " + Integer.parseInt(data[0]) + ", \"P_PSTREAK\" = " + Integer.parseInt(data[1]) + ", \"P_TOTALGP\" = " + Integer.parseInt(data[2]) + ", \"P_SUSPENDED\" = " + Integer.parseInt(data[3]) + ", \"P_TRAINING\" = " + Integer.parseInt(data[4]) + ", \"P_WEIGHT\" = " + Integer.parseInt(data[5]) + ", \"P_HEIGHT\" = " + Integer.parseInt(data[6]) + ", \"P_ORGSTATUS\" = " + Integer.parseInt(data[7]) + " where \"P_ID\" = " + idindex);                        }                    }                    if (counter==11) {                        text = text.trim();                        data = text.split("  ");                        statement.executeUpdate("update \"PLAYERS\" set \"P_BESTGM\" = " + Integer.parseInt(data[0]) + ", \"P_BESTGW\" = " + Integer.parseInt(data[1]) + ", \"P_BESTAS\" = " + Integer.parseInt(data[2]) + ", \"P_BESTPT\" = " + Integer.parseInt(data[3]) + ", \"P_BESTGO\" = " + Integer.parseInt(data[4]) + " where \"P_ID\" = " + idindex);                    }                    if (counter==13) {                        if (text.matches("((\\w|\\s)+)(['])((\\w|\\s)+)")) {                            int apostrophe;                            String nameBefore;                            String nameAfter;                            apostrophe = text.indexOf("\'");                            nameBefore = text.substring(0,apostrophe);                            nameAfter = text.substring(apostrophe);                            statement.executeUpdate("update \"PLAYERS\" set \"P_NAME\" = \'" + nameBefore + "\'" + nameAfter + "\' where \"P_ID\" = " + idindex);                        }                        else {                            statement.executeUpdate("update \"PLAYERS\" set \"P_NAME\" = \'" + text + "\' where \"P_ID\" = " + idindex);                        }                    }                    if (counter==16) {                        data = new String[13];                        data[0] = text.substring(0,3);                        data[1] = text.substring(3,6);                        data[2] = text.substring(6,9);                        data[3] = text.substring(9,12);                        data[4] = text.substring(12,15);                        data[5] = text.substring(15,18);                        data[6] = text.substring(18,21);                        data[7] = text.substring(21,24);                        data[8] = text.substring(24,27);                        data[9] = text.substring(27,30);                        data[10] = text.substring(30,33);                        data[11] = text.substring(33,36);                        data[12] = text.substring(36,39);                        statement.executeUpdate("update \"PLAYERS\" set \"P_CFIGHTING\" = " + Integer.parseInt(data[0]) + ", \"P_CSHOOTING\" = " + Integer.parseInt(data[1]) + ", \"P_CPLAYMAKING\" = " + Integer.parseInt(data[2]) + ", \"P_CHANDLING\" = " + Integer.parseInt(data[3]) + ", \"P_CCHECKING\" = " + Integer.parseInt(data[4]) + ", \"P_CMARKING\" = " + Integer.parseInt(data[5]) + ", \"P_CHITTING\" = " + Integer.parseInt(data[6]) + ", \"P_CSKATING\" = " + Integer.parseInt(data[7]) + ", \"P_CENDURANCE\" = " + Integer.parseInt(data[8]) + ", \"P_CPENALTY\" = " + Integer.parseInt(data[9]) + ", \"P_CFACEOFFS\" = " + Integer.parseInt(data[10]) + ", \"P_CLEADERSHIP\" = " + Integer.parseInt(data[11]) + ", \"P_CSTRENGTH\" = " + Integer.parseInt(data[12]) + " where \"P_ID\" = " + idindex);                    }                    if (counter==19) {                        text = text.trim();                        data = text.split("  ");                        statement.executeUpdate("update \"PLAYERS\" set \"P_ATTITUDE\" = " + Integer.parseInt(data[0]) + ", \"P_APOS\" = " + Integer.parseInt(data[1]) + ", \"P_NHLRIGHTS\" = " + Integer.parseInt(data[2]) + ", \"P_INJPRONE\" = " + Integer.parseInt(data[3]) + ", \"P_DRAFTO\" = " + Integer.parseInt(data[4]) + " where \"P_ID\" = " + idindex);                    }                }            }            fileInput.close();        }        catch(SQLException sqle) {            sqle.printStackTrace();        }        catch(ClassNotFoundException cnfe) {            cnfe.printStackTrace();        }        finally {            try {                statement.close();                connection.close();            }            catch(Exception e) {                e.printStackTrace();            }        }    }}

Thanks in advance,CBEdit: I put this is in the "Web Services" subforum without realizing it was under the "XML Forums" parent... there's no real Java forum, so this might have to get moved appropriately. mea culpa

Link to comment
Share on other sites

  • 2 months later...

I think that you need to get a web server that will support J2EE, I dont know much about JAVA so I havent really read your code..

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...