【发布时间】: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