Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从txt文件中读取数据,并使用java将其插入数据库_Java - Fatal编程技术网

从txt文件中读取数据,并使用java将其插入数据库

从txt文件中读取数据,并使用java将其插入数据库,java,Java,我想从txt文件中读取数据并将其插入数据库,但我的代码只插入16行并停止。txt文件有200行。 我不知道我做错了什么。 任何帮助都将不胜感激。提前谢谢 这是我的密码: public static void main(String[] args) throws ClassNotFoundException, SQLException { String date; String heure; String parametre; String valeur;

我想从txt文件中读取数据并将其插入数据库,但我的代码只插入16行并停止。txt文件有200行。 我不知道我做错了什么。 任何帮助都将不胜感激。提前谢谢

这是我的密码:

public static void main(String[] args) throws ClassNotFoundException, SQLException
{
    String date;
    String heure;
    String parametre;
    String valeur;
    PreparedStatement ps = null;
    Connection con = null;
    ResultSet rs = null;

    try
    {
        BufferedReader br = new BufferedReader(new FileReader("Data_Station_1.txt"));
        String username = "postgres";
        String pwd = "elghorrim";
        String connurl = "jdbc:postgresql://localhost:5432/BDS_CSF_AuqliteEau";

        con = DriverManager.getConnection(connurl, username, pwd);
        Class.forName("org.postgresql.Driver");

        String line = null;
        while ((line = br.readLine()) != null)
        {
            String tmp[] = line.split(",");
            date = tmp[0];
            heure = tmp[1];
            parametre = tmp[2];
            valeur = tmp[3];

            System.out.println(date + "\t" + heure + "\t" + parametre + "\t" + valeur);
            String sql =
                    "INSERT INTO resultat (date_resultat,valeur,code_pc,code_parametre,heure_resultat) values ('"
                            + date + "','" + valeur + "','1','" + parametre + "','" + heure +
                            "')";

            ps = con.prepareStatement(sql);
            ps.executeUpdate();
        }

        br.close();
        con.close();
        ps.close();

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

你的代码看起来是可行的,不是最好的方法,但应该是可行的

您没有对该文件进行任何类型的验证,我猜该文件存在问题。我不能确定,因为你没有发送文件

一些建议:

  • 创建一个连接类,这样您就不需要数据库用户、密码等。。。每次需要做与数据库相关的事情时

  • 连接池也很棒

干杯

//更新示例:

     //create a class that will open your connection and do other sql stuff for you 
 //so you busineess rules will be more readable  
 SQLHelper sqlHelper = new SQLHelper();
 sqlHelper.startCON();

String line = null;
while((line=br.readLine()) != null){
String[] tmp=line.split(",");

//I will check here the size of the line readed, I do not know how is a common size so I am using 10 chars, 
//change it as your needs, you also need to validate is the split worked and found your 4 fields
if(line.length()>9 && tmp.length==4)
{
    sqlHelper.toResultat(tmp);
}

第一个问题是第17行不包含任何内容

现在来看第二个(管理空行):


请格式化您的代码以提高可读性。阅读如何(重新)使用准备好的语句()检查第17行,同时检查回车符和编码,可能是重音字符谢谢@flafoux您是对的,第17行不包含任何数据,但第18行包含。所以我想我应该改变“while”条件!您知道我应该怎么做而不是[while((line=br.readLine())!=null)]谢谢您是否在停止时收到NullPointerException或ArrayIndexOutOfBoundsException或任何其他错误?空行算作\n(cr/lf)而不是null。非常感谢您的帮助。事实上,你是对的,问题在于文件,因为第17行是空的。我应该怎么做才能跳过空行?谢谢是advanceAs,我告诉过你应该在字符串上添加验证。您可以检查字符串长度,我还看到您拆分了行,您是否希望始终有4个字段?如果是,请验证它。我刚刚用一个例子编辑了我的答案,如果你喜欢,请不要忘记投票。
while((line=br.readLine())){

//Make sure the line is not null, not empty, and contains 3 comma char
if (line != null && !line.equals("") && line.matches('.*[,].*[,].*[,].*')) {
    String tmp[]=line.split(",");
    date=tmp[0];
    heure=tmp[1];
    parametre=tmp[2];
    valeur=tmp[3];

// do the sql query

} else {
    // manage where the line doesn't fit the pattern
}