在 JavaScript 中通过 JDBC 连接到 mysql
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24427018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connect to mysql via JDBC in JavaScript
提问by Ryan-Neal Mes
I am trying to pull data from a MySql database into a Google Doc via Apps Script. When I try connect to the database I get the error below because Jdbc is undefined.
我正在尝试通过 Apps 脚本将数据从 MySql 数据库提取到 Google Doc。当我尝试连接到数据库时,我收到以下错误,因为 Jdbc 未定义。
Cannot read property 'getConnection' of undefined
Below is the code I am using
下面是我正在使用的代码
<div>
<span id="import" style="background-color: red">Import</span>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
/**
* On document load, assign click handlers to each button and try to load the
* user's origin and destination language preferences if previously set.
*/
$(function() {
$('#import').click(import_test);
});
// Replace the variables in this block with real values.
var address = 'ip';
var user = 'username';
var userPwd = 'password';
var db = 'db';
var dbUrl = 'jdbc:mysql://' + address + '/' + db;
// Read up to 1000 rows of data from the table and log them.
function readFromTable() {
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
}
function import_test() {
console.log('ping');
readFromTable();
console.log('ping2');
}
</script>
The documentation is only for Google Cloud SQL and I can't find anything in the mysql documentation on how to do this in javascript.
该文档仅适用于 Google Cloud SQL,我在 mysql 文档中找不到有关如何在 javascript 中执行此操作的任何内容。
Any ideas on how to connect to MySql via Apps Script?
关于如何通过 Apps 脚本连接到 MySql 的任何想法?
Thanks.
谢谢。
采纳答案by Zig Mandel
As with most frameworks, all database calls should be done from the server, not the browser.
与大多数框架一样,所有数据库调用都应该从服务器而不是浏览器完成。
Put it on the server and call a server function from the client js.
放到服务端,从客户端js调用一个服务端函数。