【问题标题】:Java string/stringbuilder inserts null in MySql databaseJava string/stringbuilder 在 MySql 数据库中插入 null
【发布时间】:2016-01-30 12:20:36
【问题描述】:

Java String 总是将 null 发送到 MySQL 数据库。

我有一个程序可以在命令提示符下正常运行并提供所需的输出,但是当我将值保存到数据库时它总是保存 null。

void hddName(String ip) {
 
  try {
         String commands="cmd /c wmic.exe DISKDRIVE  get name";//HDD Details
         //System.out.println({"CMD", "/C", "WMIC OS GET Installdate,SerialNumber"});
         Process process = Runtime.getRuntime().exec(commands);
         process.getOutputStream().close(); //Closing output stream of the process
         System.out.println();
         StringBuilder sb=new StringBuilder();
        //Reading sucessful output of the command
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    while ((s = reader.readLine()) != null)
    {
        sb.append(s).toString();//append output 
    }
        System.out.println("result"+sb);//gives result with null keyword
        String ss=sb.toString();//gives result with null keyword
        System.out.println("result"+ss);
        try{
                System.out.println(s);
                Connection conn;
                Statement stmt;
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/system","root","");
                stmt= conn.createStatement();
                String sql="update sys set hdname='"+ss+"' where ip='"+ip+"'";// save null to mysql database
                int val= stmt.executeUpdate(sql);  
                conn.close();
                stmt.close();
            }
            catch(Exception e)
            {
                
            }
    // Reading error if any
    reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
    while ((s = reader.readLine()) != null)
    {
     System.out.println(s);//any error
    }
   } 
  catch (IOException e)
 {
   e.printStackTrace(); //TODO: necessary exception handling
  }
  // return s;
 }

【问题讨论】:

  • 顺便说一句,题外话:你不应该打电话给toString()sb.append(s).toString();//append output

标签: java mysql string stringbuilder


【解决方案1】:

在您的系统输出中打印 null 吗? sb 总是 null 吗?

    System.out.println("result"+sb);//gives result with null keyword
    String ss=sb.toString();//gives result with null keyword
    System.out.println("result"+ss);

试试

String ss= "";
    //Reading sucessful output of the command
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getOutputStream()));
while ((s = reader.readLine()) != null)
{
    ss = ss + s.toString();//append output 
}

希望对你有帮助。

【讨论】:

  • 它像 Name \\.\PHYSICALDRIVE0 null 一样打印,但是当我将它插入 mysql 数据库时,它只插入 NULL
  • 它的打印类似于 ---------{ Name \\.\PHYSICALDRIVE0 null } ------------------ 但是当我将它插入到 mysql 数据库中,它只插入 NULL
  • 我不确定您需要保存什么,但是...您是否尝试过:“process.getOutputStream()”而不是“process.getInputStream()”?
  • 我只想保存名称 \\.\PHYSICALDRIVE0
  • 我只想保存名称 \\.\PHYSICALDRIVE0 一切正常,除了它在数据库中保存结果 null 意味着在结果覆盖写入字符串值之后,在插入数据库之前为 null。我只想在插入数据库之前删除 null 或阻止它覆盖
【解决方案2】:

永远不要将值作为 SQL 的一部分嵌入。您正在使您的程序容易受到 SQL 注入的攻击。

执行以下操作:

stmt= conn.prepareStatement("update sys set hdname= ? where ip=?");
stmt.setString(1, ss);
stmt.setString(2, ip);
stmt.executeUpdate();

如果您确定ss 不是null,那么问题可能是由您的方法构造的奇怪SQL 引起的。

【讨论】:

    【解决方案3】:

    这是因为 BufferedReader。因为我是从缓冲区读取的。最后将 null 放入变量中。因此,我首先将输出写入文件,然后从中读取,然后保存到数据库。

    【讨论】:

      猜你喜欢
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 2018-05-04
      相关资源
      最近更新 更多