【发布时间】:2011-08-23 06:21:24
【问题描述】:
我是 Java 新手,我需要一些关于如何调试我的 Java Applet 的建议/信息。
我创建了一个简单地更新 MySQL 数据库的小程序。小程序似乎没有错误地加载到网页中。当我单击按钮更新数据库时,它似乎实际上调用了小程序,但没有任何反应,即没有对数据库进行新插入,并且页面正确返回。
我已获取小程序代码并在 Java 桌面应用程序中对其进行了测试。它工作正常,除了删除“扩展小程序”修饰符之外没有任何变化。在桌面应用程序中,数据库会正确更新。
如果我得到了一些关于如何写入 Java 控制台窗口的指示,这可能有助于我调试代码 - 但我不知道该怎么做。我不确定还有什么可以尝试找到问题。一切对我来说似乎都是正确的。
顺便说一句:我在 Windows 7 中使用 Netbeans 6.7 和 MySQL 服务器,在 CENTOS (Linux) 系统上使用 Glassfish 2.1。
这是我的小程序代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.me.db;
import java.applet.*;
import java.sql.*;
/**
*
* @author Rick
*/
public class dbapplet extends Applet {
/**
* Initialization method that will be called after the applet is loaded
* into the browser.
*/
public void init() {
// TODO start asynchronous download of heavy resources
}
public long SaveToDatabase(String subject, String note, int priority,
String category, String isOpen, String currentSession) {
Connection con = null;
Statement stmt = null;
StringBuilder sb = new StringBuilder();
long lastInsertId = -1;
try {
//build the insert
int IsOpen = (isOpen.contains("1")) ? 1 : 2;
sb.append("INSERT INTO 'LogDetails' ('category', 'priority',
'subject', 'note', 'is_open', 'has_attachements') VALUES");
sb.append(" (");
sb.append("'" + category + "',");
sb.append(priority + ",");
sb.append("'" + subject + "',");
sb.append("'" + note + "',");
sb.append("b'" + IsOpen + "',");
sb.append("b'0');");
//connect and execute the insert
String dbURL = "jdbc:mysql://localhost/authentication";
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(dbURL, "xxxxxxx", "yyyyyyyy");
stmt = con.createStatement();
stmt.execute(sb.toString());
//get the last inserted id
ResultSet rs = null;
rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");
if (rs.next()) {
lastInsertId = rs.getLong(1);
}
rs.close();
} catch (Exception e) { //database problem
System.out.println("Error " + e.getMessage());
System.out.println(sb.toString());
}
return lastInsertId;
} //end of SaveToDatabase
public void QuickSaveToDataBase() {
//disgard the result for now - lets see if we can get this working
this.SaveToDatabase("Quick Save", "Testing of the Quick Save Function",
1, "Testing", "1", "skjdkjd-junk");
}
}
【问题讨论】:
-
网页代码看起来像这样......
-
您确定用户名和密码正确吗? ;-)
-
查看控制台上显示的异常对诊断问题有很大帮助。