SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problem getting method in external javascript

    I want to connect my jsp file to Mysql from another external javascript file.I used a db.js file to connect to db.Could anybody tell me if there is any wrong with my code.It says- conDB()[a method in .js file] is not defined in jsp file at runtime.
    Code:
    <%@ page import="java.sql.*" %>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>DATABASE CONNECTIVITY</title>
    <script type="text/javascript" src="db.js">
    </script>
    </head>
    <!--END OF HEAD-->
    <body >
    <input type="button" onclick="conDB();" value="Show Data box" />
    <div id="targetDiv">
      <p>The fetched data will go here.</p>
    </div>
    </body>
    </html>
    This is my extenal javascript file- db.js
    Code:
    function conDB(){ 
    var connectionURL="jdbc:mysql://localhost/mydb";
    var connection = null;
    var statement = null;
    var rs = null;
    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    connection = DriverManager.getConnection(connectionURL, "root", "admin");
    statement = connection.createStatement();
    rs = statement.executeQuery("SELECT * FROM mydb.user");
    if(rs.next())
    {
    	document.write("DATA FOUND!");
    }
    else {document.write("NO DATA!");}
    
    }

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is that Java code you're trying to run in db.js?

    (BTW, using document.write() like that in an event handler wouldn't be a great idea; it will replace the page.)
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo View Post
    Is that Java code you're trying to run in db.js?

    (BTW, using document.write() like that in an event handler wouldn't be a great idea; it will replace the page.)
    Yes bro. It is the java code in db.js file.I m a newbie so I can be wrong.Cant I use any type of java code in javascript just as we do in normal java file?If not,then how to use code in javascript then.Why is it not getting the method in external javascript file.I think there is something wrong in my conDB() function.Can we suggest me a code in db.js to connect to database.

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't have Java code in JavaScript. They are two separate languages with more differences than similarities. It's called JavaScript because the syntax is similar (could just as well have been called CScript or C++Script, but Java was the buzzword at the time). In fact, at first it was called LiveScript, then it was standardised as ECMAScript. The term JavaScript is very common, but rather inofficial.

    You cannot talk directly to your server-side database from client-side JavaScript. You can use something like ajax to send a request via JavaScript to a script or application on your server, which can then extract information from the database and return the result as, e.g., XML or JSON to your client-side script.
    Birnam wood is come to Dunsinane

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,840
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by AutisticCuckoo View Post
    It's called JavaScript because the syntax is similar
    Before Java became popular JavaScript was called LiveScript. It was renamed JavaScript to try to benefit from the publicity that Java was getting. I think there was even a short period in between when they called it MoccaScript.

    Also you can interface directly to Java from JavaScript but the interface is very limited and I have never come across anyone who has actually used it.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, this question might do better in the Java forum. In the JSP page you can have Java, so I see why you might think you could have it in the Js... but I think the problem is because the Js file, being external to the JSP page (as you noted), is not a part of the Servlet (the JSP page). I'm not a Servlet/JSP guru so I could be wrong.

    You can put Java that makes the DB access directly in that page, and have it generate an array of data in Javascript. That might be one way to do it.


Bookmarks

Posting Permissions

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