0

我有我写的这个程序,但我遇到了问题。我只使用“插入”开始了这个项目,没有“重复键”,它工作得很好。现在自从我添加它以来,代码进入一个循环并且不执行任何东西。我从文件中读取此代码并插入数据库。我尝试了更新,但如果该文件中有新项目,它就不起作用。谁能告诉我我做错了什么。

这是我的代码

import java.sql.*;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

 public class mysql {
static Connection conn;
public static void main (String argv[]) throws SQLException, ClassNotFoundException     {
File file = new File("/home/bandoc/data.txt");

if (!file.exists()) {
    System.out.println("Cannot Read File\n");
}
BufferedReader br = null;
//connect to mysql db
Class.forName ("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost/membership?  user=My-Username&password=my-Password");
Statement stmt = conn.createStatement();
ResultSet mysqldata = stmt.executeQuery ("select * from logIn");

try {
    br = new BufferedReader(new FileReader(file.getPath()));    
}catch(IOException e) {
    e.printStackTrace();
}

StringBuilder sb = new StringBuilder();
String line = "";
try {
    line = br.readLine();

}
catch(IOException e) {
    e.printStackTrace();
}

PreparedStatement stm =  conn.prepareStatement("insert into logIn (logId, logFname, logLname, logDept, logUser, logEmail, logDeptCode) values (?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE test =1");

while (line != null) {
    if (line.equalsIgnoreCase("ID:")) {
        try {
            line = br.readLine();

            stm.setInt(1,Integer.parseInt(line.toString()));
            line = br.readLine();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (line.equalsIgnoreCase( "First:")) {
        try {
            line = br.readLine();

            stm.setString(2,line);
            line = br.readLine();
         }catch (IOException e) {
                            e.printStackTrace();
                     }

    }
    if (line.equalsIgnoreCase("Last:")) {
        try {
            line = br.readLine();

            stm.setString(3,line);
            line = br.readLine();
         }catch (IOException e) {
                            e.printStackTrace();
                     }

    }
    if (line.equalsIgnoreCase("Dept:")) {
        try {
                        line = br.readLine();

                        stm.setString(4,line);
                        line = br.readLine();
         }catch (IOException e) {
                            e.printStackTrace();
                     }


    if (line.equalsIgnoreCase( "User:")) {
                    try {
                            line = br.readLine();

                            stm.setString(5,line);
                            line = br.readLine();
                     }catch (IOException e) {
                            e.printStackTrace();
                     }
        }

    if (line.equalsIgnoreCase( "Email:")) {
                    try {
                            line = br.readLine();

                            stm.setString(6,line);
                            line = br.readLine();
                     }catch (IOException e) {
                            e.printStackTrace();
                     }
        }


    if (line.equalsIgnoreCase( "Deptcode:")) {
                    try {
                            line = br.readLine();

                            stm.setString(7,line);
                            //line = br.readLine();
                     }catch (IOException e) {
                            e.printStackTrace();
                     }

        }   


    }
    stm.execute();
}
 try {
            br.close();
    }catch(IOException e) {
            e.printStackTrace();
    }



    System.out.println("done");

}
  }
4

2 回答 2

0

If your 'line' does not equal one of the conditions e.g. 'ID:', then the next readline is not done and 'line' remains the same, hence a never ending loop

于 2013-05-24T00:29:58.367 回答
0

我的猜测是你永远不会离开你的 while 循环。你读了第一行,然后进入一个循环,当读入的行是时停止null。您在循环中有一堆条件来检查该行并在它匹配某个条件时读取另一行。因此,如果读取的第一行不是null,并且第一行不匹配循环中的条件,那么您将永远陷入该循环。一点调试将证明(或反驳这一点),else底部的最终条件可能会记录您遇到意外的行(并打印它),然后继续阅读下一行。

于 2013-05-23T21:24:55.737 回答