简体   繁体   English

是否可以仅使用javascript检索数据库详细信息

[英]Is it possible to retrive database details using javascript only

I am newbie to javascript. 我是javascript的新手。 I am just wondering whether is it possible to fetch database details using only javascript. 我只是想知道是否有可能仅使用javascript来获取数据库详细信息。 I know javascript is client side component. 我知道javascript是客户端组件。 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.? 是否可以仅使用javascript来获取同一内容,而javascript可以在任何计算机(如Windows,Linux和Mozilla,IE,Safari等)上运行? Any pointer, suggestion really helpful me to understand power of javascript. 任何指针,建议确实有助于我理解javascript的功能。

No, it is not possible using client-side javascript. 不,无法使用客户端javascript。

There are server side javascript options like node.js . 有服务器端的javascript选项,例如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. 借助HTML5的websockets功能,您可以使用JavaScript连接到特定主机并通过网络进行通信。 So, it would be technically possible to connect to the database port and send queries and receive results. 因此,从技术上讲,可以连接到数据库端口并发送查询和接收结果。

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

However, HTML5 is still just a proposal and browsers are only starting to implement its features. 但是,HTML5仍然只是一个建议,浏览器才刚刚开始实现其功能。 Many currently used browsers (like older versions of IE), of course, don't implement any of the features. 当然,许多当前使用的浏览器(例如IE的较早版本)并没有实现任何功能。 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. 此外,可能没有用于JS的任何数据库驱动程序,因此您将不得不进行所有底层通信,而这可能是您不希望的。

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. 在Web应用程序中完成此操作的通常方法是JavaScript通过AJAX从服务器请求数据,然后服务器与数据库进行通信。 Those servers are often made in PHP, but you can use your Java code to make a servlet. 这些服务器通常是用PHP制造的,但是您可以使用Java代码制造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. 基本上,所需要做的就是将代码移动到HttpServletdoGet()doPost()函数,将这些类打包到Web归档文件中,然后使用Tomcat或Jetty启动它。

JavaScript can only initiate calls to server side code that in turn connects to the database. JavaScript只能发起对服务器端代码的调用,而服务器端代码又将连接到数据库。 A common technique these days is to make AJAX calls (using JavaScript) to call services that will return data from a database. 如今,一种常见的技术是进行AJAX调用(使用JavaScript)来调用将从数据库返回数据的服务。

There are several examples online that describes the technique 网上有几个描述该技术的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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