【发布时间】:2015-01-12 09:20:09
【问题描述】:
美好的一天。任何人都可以真正帮助我解决我面临的数据库问题吗?我想使用 pepraredStatement 插入数据库。但是,每当我添加数据库部分(连接和 pepraredStatement)时,“上传”按钮就会无响应。但是当我删除与数据库相关的任何内容时,我所有的按钮都在工作。你可以在这里找到代码http://pastebin.com/euKdWhr2。
我将非常感谢任何帮助或建议。可能我在数据库部分遗漏了一些东西。
这不是一个重复的问题,因为我没有找到正确的答案 公共无效actionPerformed(ActionEvent ev) { 字符串文件 = fileField.getText(); SetGetQuestionFileName 模式 = new SetGetQuestionFileName(file); ConnectToDatabase 数据库 = 新的 ConnectToDatabase(); 尝试 {
///////// check whether textfile is empty or not
if( ev.getActionCommand().equals("UPLOAD"))
{
if(fileField.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"File field can not be empty!!! Please try again.","ALERT", JOptionPane.ERROR_MESSAGE);
}
else
{
File fi = new File(fileField.getText());
//////////////// perform upload
try
{
String sql = "INSERT INTO testsystem.questionnaire (category_questions, questions, correct_answer)" + "VALUES (?, ?, ?)";
PreparedStatement st = null;
Connection dbconnection = database.getConnection();
st = dbconnection.prepareStatement(sql);
if(fi.getAbsoluteFile().exists())
{
List<String> lines = Files.readAllLines(Paths.get(fileField.getText()), Charset.defaultCharset());
for (int i = 0; i < lines.size(); i+=10)
{
String category = lines.get(i);
System.out.println(category);
String question = lines.get(i+1);
System.out.println(question);
String answers =
lines.get(i+2)+System.lineSeparator()
+lines.get(i+3)+System.lineSeparator()
+lines.get(i+4)+System.lineSeparator()
+lines.get(i+5);
System.out.println(answers);
String correct = lines.get(i+7);
System.out.println("correct answer is: "+correct);
System.out.println("----------------");
st.setString(1, category);
st.setString(2, answers);
st.setString(3, correct);
st.executeUpdate();
}
JOptionPane.showMessageDialog(null,"File has been successfully uploaded in the database.","NOTIFCATION",JOptionPane.INFORMATION_MESSAGE);
}
else
JOptionPane.showMessageDialog(null,"File could not be found. Please try again","ALERT",JOptionPane.ERROR_MESSAGE);
}
catch(SQLException ex)
{
}
catch(Exception ex)
{
}
【问题讨论】:
标签: java swing jdbc swingworker