我正在尝试做一个示例来使用 java applet 将数据库记录写入文件。但问题是在运行小程序后,我看到文件已创建但它包含垃圾数据。
我已经签署了我的小程序(按照本教程: http: //www.narendranaidu.com/2007/11/3-easy-steps-to-self-sign-applet-jar.html)
我试过把 mysql-创建的 jar (SaveToFile.jar) 中的连接器。但同样的事情发生了。所以我很困惑数据库连接是否真的发生了。
构建后,我的项目结构如下所示(这里我只显示构建的部分):
appletTest/
out/production/appletTest/
applet/SaveToFile.class
SaveToFile.html
SaveToFile.jar
mysql-connector-java-5.1.24-bin.jar
file-applet.jnlp
keystore
用火狐浏览器打开html文件后(chrome没有任何反应,不知道为什么!)并通过授予applet运行的权限,在同一目录下成功创建了data.txt文件。
这是Java代码:
package applet;
public class SaveToFile extends Applet {
public void init() {
try {
getData();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private void getData() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
String myDriver = "com.mysql.jdbc.Driver";
String dbUrl = "jdbc:mysql://localhost/APPLET?user=root&password=root&characterSet=utf8&useUnicode=true&characterEncoding=utf-8&characterSetResults=utf8";
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(dbUrl);
Statement stmt = conn.createStatement();
try {
ResultSet rs = stmt.executeQuery( "SELECT * FROM applet" );
try {
String text = "";
while ( rs.next() ) {
text += rs.getString("title");
}
writeToFile(text);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
} catch (Throwable ignore) {
ignore.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
} catch (Throwable ignore) {
ignore.printStackTrace();
}
}
}
public void writeToFile (String text) {
try {
DataOutputStream dataOut = new DataOutputStream(new FileOutputStream("data.txt"));
dataOut.writeUTF(text);
dataOut.close();
} catch(Exception x) {
x.printStackTrace();
}
}
}
这是jnlp:
<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">
<information>
<title>Write To File</title>
<vendor>Vendor</vendor>
</information>
<resources>
<!-- Application Resources -->
<j2se version="1.7+"
href="http://java.sun.com/products/autodl/j2se"
max-heap-size="128m"/>
<jar href="SaveToFile.jar" main="true"/>
</resources>
<applet-desc name="Test Applet"
main-class="applet.SaveToFile"
width="22"
height="22"/>
<update check="background"/>
<security>
<all-permissions/>
</security>
</jnlp>
这是html:
<!DOCTYPE html>
<html>
<head>
<title>Applet Test</title>
</head>
<body>
<script src="http://java.com/js/deployJava.js"></script>
<script type="text/javascript">
var attributes = { code:'applet.SaveToFile', width:22, height:22} ;
var parameters = {jnlp_href: 'file-applet.jnlp'} ;
deployJava.runApplet(attributes, parameters, '1.7');
</script>
<applet
id="saveToFile"
width=22
height=22
code="applet.SaveToFile"
archive="SaveToFile.jar"
jnlp_href = 'file-applet.jnlp'
/>
</body>
</html>
最后是mysql表:
+----+--------+
| id | title |
+----+--------+
| 1 | TITLE1 |
| 2 | TITLE2 |
| 3 | TITLE3 |
| 4 | TITLE4 |
+----+--------+
即使我已经尝试过这个查询(如下:http ://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/ )但没有这次发生了:
SELECT * FROM applet
INTO OUTFILE 'tmp/data.txt'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
此外,如果有人可以举一个例子来说明在处理 javascript 和 applet 之间的数据库交互方面的安全性,那将会很有帮助。
谢谢你