Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.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
Java字符串/stringbuilder在MySql数据库中插入null_Java_Mysql_String_Stringbuilder - Fatal编程技术网

Java字符串/stringbuilder在MySql数据库中插入null

Java字符串/stringbuilder在MySql数据库中插入null,java,mysql,string,stringbuilder,Java,Mysql,String,Stringbuilder,Java字符串总是向MySQL数据库发送null 我有一个程序,可以在命令提示符下正常工作,并提供所需的输出,但当我将值保存到数据库时,它总是保存null void hddName(String ip) { try { String commands="cmd /c wmic.exe DISKDRIVE get name";//HDD Details //System.out.println({"CMD", &

Java字符串总是向MySQL数据库发送null

我有一个程序,可以在命令提示符下正常工作,并提供所需的输出,但当我将值保存到数据库时,它总是保存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;
 }

是否在系统输出中打印空值?某人总是空的吗

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


希望有帮助。

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

做一些类似于:

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

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

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

它像Name\\\一样打印。\PHYSICALDRIVE0null,但当我将它插入mysql数据库时,它只是insert null,它的打印方式是这样的-----------{Name\\.\PHYSICALDRIVE0 null}--------------但当我将它插入mysql数据库时,它只是插入null,我不确定需要保存什么,但是。。。您尝试过“process.getOutputStream()”而不是“process.getInputStream()”吗?我只想保存名称\\.\PHYSICALDRIVE0我只想保存名称\\.\PHYSICALDRIVE0除在数据库中保存结果null外,一切正常表示结果重写后为null插入数据库之前的字符串值。我只想在插入数据库之前删除null或停止它的过度写入。顺便说一句,有点离题:您不应该在
sb.append(s.toString())处调用
toString()
//追加输出
stmt= conn.prepareStatement("update sys set hdname= ? where ip=?");
stmt.setString(1, ss);
stmt.setString(2, ip);
stmt.executeUpdate();