0

I have the following code snippet. I have tried all the possible things mentioned on StackExchange but am unable to spot the error in my code.

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8" />
<title>Twitter Mapper</title>
<link rel="stylesheet" href="styles/styles.css">

<script type="text/javascript"
    src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
</head>
<body>

    <br />
    <br />
    <div id="map-canvas" align="center"></div>

    <script src="scripts/heatmap.js"></script>
    <script src="scripts/gmaps-heatmap.js"></script>
    <script>
    window.onload = (function() {

        alert("hi");
        // map center
        var myLatlng = new google.maps.LatLng(25.6586, -80.3568);
        // map options,
        var myOptions = {
            zoom : 3,
            center : myLatlng
        };
        // standard map
        map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
        // heatmap layer
        heatmap = new HeatmapOverlay(map, {
            // radius should be small ONLY if scaleRadius is true (or small radius is intended)
            "radius" : 2,
            "maxOpacity" : 1,
            // scales the radius based on map zoom
            "scaleRadius" : true,
            // if set to false the heatmap uses the global maximum for colorization
            // if activated: uses the data maximum within the current map boundaries 
            //   (there will always be a red spot with useLocalExtremas true)
            "useLocalExtrema" : true,
            // which field name in your data represents the latitude - default "lat"
            latField : 'lat',
            // which field name in your data represents the longitude - default "lng"
            lngField : 'lng',
            // which field name in your data represents the data value - default "value"
            valueField : 'count'
        });
    });

        function populateMap() {

            Connection conn = null; 
             Statement stmt = null; 
             ResultSet rset = null; 
             try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                conn = DriverManager.getConnection("jdbc:mysql://localhost:8889/mysql", 
                                                   "", "");
                stmt = conn.createStatement();
                // dynamic query
                rset = stmt.executeQuery ("SELECT * FROM tweets");
               //return (formatResult(rset));
             } finally {
                 if (rset!= null) rset.close(); 
                 if (stmt!= null) stmt.close();
                 if (conn!= null) conn.close();
             }

        };


    </script>
</body>
</html>

The line for the error is shown to be as the first line in function populateMap(). Please help.

Mouser
  • 13,132
  • 3
  • 28
  • 54
BW12
  • 13
  • 1
  • 6
  • 2
    What is `Connection conn = null; Statement stmt = null; ResultSet rset = null; ....`? Looks more like Java than JavaScript. – epascarello Mar 09 '15 at 15:17
  • 1
    Are you trying to cast in JavaScript?, Just replace those object definitions and with `var`. Should fix the syntax error. – Mouser Mar 09 '15 at 15:19
  • 2
    You seem to be mixing languages. [tag:Java] is to [tag:JavaScript], as car is to carpet ([quote](http://stackoverflow.com/a/245068/444991)). – Matt Mar 09 '15 at 15:21
  • I have imported the java.sql on top hence, was using those variables. I will try and replace those with var. – BW12 Mar 09 '15 at 15:22
  • What has `Class.forName("....")` to do with JavaScript ??? – cнŝdk Mar 09 '15 at 15:24
  • You cant call java code from javascript, even if you think you've imported the packages – Jamiec Mar 09 '15 at 15:25
  • `populateMap` is not written in JavaScript. – sebnukem Mar 09 '15 at 15:25
  • Okay so I was able to load the page by changing all those types to var. – BW12 Mar 09 '15 at 15:25
  • @chsdk I am trying to fetch data from mysql database. isn't it the right way to do it? – BW12 Mar 09 '15 at 15:27
  • But it's Java and not Javascript, honestly I don't have any idea of how to access database using Javascript, but you can take a look at [this](http://stackoverflow.com/questions/857670/how-to-connect-to-sql-server-database-from-javascript), hope it helps. – cнŝdk Mar 09 '15 at 15:45
  • Okay. Thanks a lot for sharing that. I will see if I can implement that. – BW12 Mar 09 '15 at 15:49

2 Answers2

0

in Javascript you don't declare variables using type, but using var:

var conn = null, stmt = null, rset = null; 
Pavel Gatnar
  • 3,987
  • 2
  • 19
  • 29
0

The commenters are right, but looking at the lines past that, that may not be your only issue. Unless this is some really powerful JavaScript library I don't know about that somehow gets included in Google's map scripts, then I don't think the JDBC things you're trying to do actually exist.

JavaScript runs entirely on the user's browser; so even if JS had a built-in database querying library, chances are the database would refuse the direct connection. You likely need to create that database query on the server as an AJAX endpoint that converts its data into a text format like JSON, and lets the JavaScript request that using AJAX.

I can't go over all of that in a single post, but you have a lot of reading up to do. I'd start with a basic tutorial on AJAX, or the differences between JavaScript and Java (it's not just night and day; it's night and tabletop tennis).

Katana314
  • 8,429
  • 2
  • 28
  • 36
  • Yes, I am trying to implement the same thing using jQuery AJAX as well. But I am short on time hence I am trying to make a quick fix by including a query in my javascript. – BW12 Mar 09 '15 at 15:28
  • 1
    @BW12 I can appreciate the need to fix something quickly, even to the extent of sometimes having security flaws, but what you're trying just doesn't really work that way. – Katana314 Mar 09 '15 at 15:31
  • I was told by a friend that I can use such a coding style if I have a mysql connector jar in my WEB-INF lib folder. I am verifying that now. – BW12 Mar 09 '15 at 15:56