Results 1 to 11 of 11
  1. #1
    phamster is offline Member
    Join Date
    Apr 2014
    Posts
    6
    Rep Power
    0

    Default Writing to SQL Database

    Hey guys,

    I created a Java applet and works perfect within Netbeans. It writes to the database and everything. Now, when I deploy it via HTML web, everything works except the part that writes to the database. I'm basically recording the player score to the SQL database. Any idea why it will not record to the database?

    Here's my connection to the database:

    Java Code:
    try
                        {
                            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                        }
                        catch (Exception e)
                        {
                            System.out.println("Failed to load mSQL driver.");
                        }
    
                        Connection connection = DriverManager.getConnection("jdbc:sqlserver://MyServer;databaseName=OrbitBlast;user=MyUserName;password=MyPassword");
                        Statement statement = connection.createStatement();
                        String sqlquery = "INSERT INTO OrbitBlast VALUES ('" + UserName + "', " + gameScore.getScore() + ", " + missileFire  + ", " + enemyHit + ")";
                        statement.executeUpdate(sqlquery);
                        statement.close();
    
                        connection.close();

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: Writing to SQL Database

    An applet is only allowed to receive and send data to the server it was downloaded from, so you can't connect to any database server that is not the same as the webserver.

    View the Java console to see the stacktrace. That is a big hint to what goes wrong.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    phamster is offline Member
    Join Date
    Apr 2014
    Posts
    6
    Rep Power
    0

    Default Re: Writing to SQL Database

    Well the applet is on the same server as the database. Say XXX is my web server name. The database is also located on XXX. It still didn't write to it. Any other ideas? Do I need to change the server name in the connection string to 'localhost' or something?

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: Writing to SQL Database

    What is in your catch-clause? Did it print out any Exception? Did you print the SQL statement to see if it was correct, both syntactically and semantically?

    You might want to consider PreparedStatements:
    Java Code:
    Connection c = ...;
    PreparedStatement ps = c.prepareStatement("insert into Orbitblast values (?,?,?,?)");
    ps.setString(1, UserName);
    ps.setInt( 2, gameScore.getScore() );
    ...
    ps.executeUpdate();
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    phamster is offline Member
    Join Date
    Apr 2014
    Posts
    6
    Rep Power
    0

    Default Re: Writing to SQL Database

    Quote Originally Posted by SurfMan View Post
    What is in your catch-clause? Did it print out any Exception? Did you print the SQL statement to see if it was correct, both syntactically and semantically?

    You might want to consider PreparedStatements:
    Java Code:
    Connection c = ...;
    PreparedStatement ps = c.prepareStatement("insert into Orbitblast values (?,?,?,?)");
    ps.setString(1, UserName);
    ps.setInt( 2, gameScore.getScore() );
    ...
    ps.executeUpdate();
    Doesnt look like it went into the catch statment. Maybe it does go into it but its not displaying the catch statememts? How do I make it pop up the catch errors within the HTML browser?

    Java Code:
    try
                    {
                        try
                        {
                            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                        }
                        catch (Exception e)
                        {
                            System.out.println("Failed to load mSQL driver.");
                        }
    
                        Connection connection = DriverManager.getConnection("jdbc:sqlserver://XXX;databaseName=OrbitBlast;user=UserName;password=MyPassword");
                        Statement statement = connection.createStatement();
                        String sqlquery = "INSERT INTO OrbitBlast VALUES ('" + UserName + "', " + gameScore.getScore() + ", " + missileFire  + ", " + enemyHit + ")";
                        statement.executeUpdate(sqlquery);
                        statement.close();
    
                        connection.close();
                    }
                    catch(Exception e)
                    {
                        String lblEnemyKilled = "Enemy Killed: " + e.toString();
                        myG.drawString(lblEnemyKilled, 250, myDimension.height - 475);
                    }

  6. #6
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Writing to SQL Database

    Did you open the java console? The output of System.out.println should be displayed in there if I remember correctly.

    Konrad

  7. #7
    phamster is offline Member
    Join Date
    Apr 2014
    Posts
    6
    Rep Power
    0

    Default Re: Writing to SQL Database

    Quote Originally Posted by kneitzel View Post
    Did you open the java console? The output of System.out.println should be displayed in there if I remember correctly.

    Konrad
    It will not error out in netbeans or running locally. Everything works, it just don't work when we distribute it to the web server so there is no way to debug.

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: Writing to SQL Database

    - Deploy the applet to the webserver
    - Pick any random workstation
    - Open de Java Console (Start -> Control Panel -> Java -> Tab "Advanced" -> Show console)
    - Open the HTML page that serves the applet
    - ...
    - Profit
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    phamster is offline Member
    Join Date
    Apr 2014
    Posts
    6
    Rep Power
    0

    Default Re: Writing to SQL Database

    Quote Originally Posted by SurfMan View Post
    - Deploy the applet to the webserver
    - Pick any random workstation
    - Open de Java Console (Start -> Control Panel -> Java -> Tab "Advanced" -> Show console)
    - Open the HTML page that serves the applet
    - ...
    - Profit
    Got it to pull up the console! So here is the error I got:

    Java Code:
    xception in thread "Thread-17" java.lang.ExceptionInInitializerError
    	at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1030)
    	at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
    	at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
    	at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
    	at java.sql.DriverManager.getConnection(Unknown Source)
    	at java.sql.DriverManager.getConnection(Unknown Source)
    	at ObitBlast.OrbitBlast.paint(OrbitBlast.java:238)
    	at ObitBlast.OrbitBlast.run(OrbitBlast.java:109)
    	at java.lang.Thread.run(Unknown Source)
    Caused by: java.security.AccessControlException: access denied ("java.util.PropertyPermission" "java.home" "read")
    	at java.security.AccessControlContext.checkPermission(Unknown Source)
    	at java.security.AccessController.checkPermission(Unknown Source)
    	at java.lang.SecurityManager.checkPermission(Unknown Source)
    	at sun.plugin2.applet.AWTAppletSecurityManager.checkPermission(Unknown Source)
    	at java.lang.SecurityManager.checkPropertyAccess(Unknown Source)
    	at java.lang.System.getProperty(Unknown Source)
    	at com.microsoft.sqlserver.jdbc.TDSChannel.<clinit>(IOBuffer.java:1432)
    I thought it might be folder permission but I granted it full rights and still the same error occur. Anyway to fix this?

  10. #10
    phamster is offline Member
    Join Date
    Apr 2014
    Posts
    6
    Rep Power
    0

    Default Re: Writing to SQL Database

    Got it solve, I had to digital sign my applet.

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: Writing to SQL Database

    Glad you got it working. Thanks for the feedback.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. reading and writing txt. files into a database
    By Dan C in forum New To Java
    Replies: 1
    Last Post: 05-30-2013, 09:51 AM
  2. Create registration form | insert into database | call the database
    By FamilyGuy in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-23-2012, 12:08 PM
  3. HELP with writing to a database with java
    By kd7vea in forum Java Applets
    Replies: 1
    Last Post: 05-12-2011, 10:32 AM
  4. Replies: 1
    Last Post: 02-27-2010, 02:29 AM
  5. editing jTable then writing new values to database!
    By sweet angle in forum AWT / Swing
    Replies: 6
    Last Post: 01-27-2009, 06:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •