简体   繁体   中英

Is it possible to retrive database details using javascript only

I am newbie to javascript. I am just wondering whether is it possible to fetch database details using only javascript. I know javascript is client side component. Normally using method we fetch database details.

public static void main(String args[]) throws SQLException {
        //URL of Oracle database server
        String url = "jdbc:oracle:thin:@localhost:1632:XE";

        //properties for creating connection to Oracle database
        Properties props = new Properties();
        props.setProperty("user", "scott");
        props.setProperty("password", "tiger");

        //creating connection to Oracle database using JDBC
        Connection conn = DriverManager.getConnection(url,props);

        String sql ="select sysdate as current_day from dual";

        //creating PreparedStatement object to execute query
        PreparedStatement preStatement = conn.prepareStatement(sql);

        ResultSet result = preStatement.executeQuery();

        while(result.next()){
            System.out.println("Current Date from Oracle : " +         result.getString("current_day"));
        }
        System.out.println("done");

    }
}

Is it possible to fetch same thing using only javascript which works on any machine like windows,linux and any browsers like mozilla, IE, Safari etc.? Any pointer, suggestion really helpful me to understand power of javascript.

No, it is not possible using client-side javascript.

There are server side javascript options like node.js . You can take a look at those.

With the websockets feature of HTML5, you can use JavaScript to connect to a certain host and communicate via network. So, it would be technically possible to connect to the database port and send queries and receive results.

https://developer.mozilla.org/en/WebSockets

However, HTML5 is still just a proposal and browsers are only starting to implement its features. Many currently used browsers (like older versions of IE), of course, don't implement any of the features. Besides, there probably aren't any database drivers for JS, so you would have to do all the communication low-level, which you probably don't want to.

The usual way how this is done in web applications is that JavaScript requests the data by AJAX from a server, which then communicates with the database. Those servers are often made in PHP, but you can use your Java code to make a servlet. Basically, all it takes is to move your code to a doGet() or doPost() function of a HttpServlet , pack the classes into a web archive and launch it with Tomcat or Jetty.

JavaScript can only initiate calls to server side code that in turn connects to the database. A common technique these days is to make AJAX calls (using JavaScript) to call services that will return data from a database.

There are several examples online that describes the technique

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM