Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Accessing local DB

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Accessing local DB

    Hi All,

    I need to store/retrieve data in a local environment. Everything is built as web pages controlled by JavaScript(s), and there is no web server running.

    As I'm using FireFox, I figured out that my only way to talk to an SQL engine will be through JDBC.

    Here is my attempt to do it:
    // the Java applet
     
    import java.applet.Applet;
    import java.sql.*;
     
    public class Query extends Applet
    { public Connection conn = null;
     
      public void start() 
      { try
        { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
          conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://servername:1433;DatabaseName=dbname","username","password");
    	}
    	catch (Exception e)
    	{ e.getMessage(); // -> ?
    	}
      }
     
      public String[][] qSelect(String qry)
      { ResultSet rs;
        try
        { Statement stmt = conn.createStatement();
          rs = stmt.executeQuery(qry);
    	  stmt.close();
    	// transform rs in String[][] for JS
    	int rows = 0;
    	while(rs.next()) rows++;
    	rs.first();
    	ResultSetMetaData metaData = rs.getMetaData();
        int cols = metaData.getColumnCount();
    	String[][] rslt = new String[rows][cols];
    	int i=0;
    	while(rs.next())
    	{ for (int j=0;j<cols;j++)
    	    rslt[i][j] = rs.getString(j+1);
    	  i++;
    	}
    	return rslt;
        }
        catch (SQLException se)
        { String[][] rslt = new String[1][1];
          rslt[0][0] = "Exception";
    	return rslt;
        }
      }
     
      public int qOther(String qry)
      { try
        { Statement stmt = conn.createStatement();
          stmt.executeUpdate(qry);
    	  stmt.close();
    	  return 1;
    	}
    	catch (SQLException se)
    	{ return 0;
    	}
      }
     
      public void stop()
      { try
        { conn.close();
    	}
    	catch (SQLException e)
    	{ e.printStackTrace();  // -> ?
    	}
      }
    }

    HTML Code:
    <!-- and the HTML part -->
    <html>
    <body>
    <applet id="sql" code="Query.class" width=1 height=1>
    </applet>
    <script type="text/javascript">
    var rez = new Array();
    rez = document.sql.qSelect('SELECT * FROM aTable;');
    l = rez.length;
    alert(l+'\n'+rez);
    </script>
    </body>
    </html>
    The idea is to get data with qSelect, and to insert/update/delete with qOther. There is no user communication from the applet - just via the calling JavaScript code.

    In the status bar I get "Applet Query started", but nothing else.

    What am I doing wrong?

    Thanks,
    SxN


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: Accessing local DB

    Are there exceptions? Did you sign the applet (see What Applets Can and Cannot Do (The Java™ Tutorials > Deployment > Java Applets) )?

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Accessing local DB

    copeg, thanks for your reply.

    Quote Originally Posted by copeg View Post
    Are there exceptions?
    There may be SQLExceptions, which I intend to pass to JavaScript, which in turn will take appropriate action

    Quote Originally Posted by copeg View Post
    Did you sign the applet
    No, I didn't. There is a LAN consisting of three client computers and a database server. They share happily their resources, and everything is visible on the client machine. As the article you pointed to says, if JavaScript calls an applet, it is like an unsigned one - but that's exactly what I need to do: call the applet from JS and retrieve the results.

    If in my configuration the DB is still considered a foreign resource, and am not allowed to work, my only choice that I can think of is to install on the DB server an Apache, and do server-side processing, but that's a complication that I'm trying to avoid. Or are there other approaches?

    Please advise,
    SxN

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Accessing local DB

    I think I'd like to rephrase a bit:

    From http://download.oracle.com/javase/tu...security.html:
    Unsigned applets can perform the following operations:
    ...
    Applets that are loaded from the local file system (from a directory in the user's CLASSPATH) have none of the restrictions that applets loaded over the network do
    The DB server's drive is mapped on the client machines, so a CLASSPATH including that drive letter should help

    Unsigned applets cannot perform the following operations:
    ...
    They cannot connect to or retrieve resources from any third party server (any server other than the server it originated from).
    As the applet resides on the same machine like the SQL Server, that shouldn't be an issue.

    Am I right? Am I wrong?

    Thanks,
    SxN

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Accessing local DB

    Solved the problem, but still don't know the right answer.

    My solution: installed Apache, installed PostgreSQL (the existing MS SQL Server was making me troubles and my time constraints are too tight to attempt fixing them), made a Python server-side script, and now JavaScript talks to Python (which talks to PostgreSQL via PyGreSQL).

    This environment is my home environment, and it works. I'm just sorry to pass an opportunity to learn some Java, but time constraints...

    SxN

Similar Threads

  1. how to search a file in local network
    By geetha in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 26th, 2011, 07:33 AM
  2. local NTP server
    By conanlive in forum Java Networking
    Replies: 1
    Last Post: August 12th, 2011, 04:46 AM
  3. JApplet Local File Permission
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: July 27th, 2011, 12:29 PM
  4. Replies: 6
    Last Post: September 19th, 2010, 08:33 PM
  5. recursive search of all local disks
    By ttsdinesh in forum Java Theory & Questions
    Replies: 4
    Last Post: September 27th, 2009, 08:23 AM