<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre code_snippet_id="588026" snippet_file_name="blog_20150124_1_2028981" name="code" class="java">JSON格式还是这篇博客中的数据: http://blog.csdn.net/lushuner/article/details/43085463
存入数据库的时候总是有各种各样的意外数据,要么字段长度溢出,要么字符串含有单引号,要么JSON格式不对,比较头疼的事情是用解析类解析出的结果要比用存入数据库的类真实存入到数据库的中结果数要多,意思就是有的JSON格式中的properties不符合要求存不进去,要从1000条的properties中人工去找哪条没存进去是不太现实的,就写了个findBug函数去找。这里对数据库的操作都用了一个SQLManager的工具类。
<pre code_snippet_id="588026" snippet_file_name="blog_20150124_4_680100" name="code" class="java">SqlManager代码:
public class SqlManager {
private static Connection conn=null;
private static PreparedStatement ps=null;
private static CallableStatement cs=null;
private static ResultSet rs=null;
public static void openConnection(){
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=json";
String userName = "sa";
String userPwd = "123";
try{
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL, userName,userPwd);
System.out.println("连接数据库成功");
}
catch (Exception e){
e.printStackTrace();
System.out.print("连接失败");
}
}
public static void closeConnection(){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean executeQuery(String sql,String []parameters){
try{
ps=conn.prepareStatement(sql);
if(parameters!=null && !parameters.equals("")){
for(int i=0; i<parameters.length; i++){
ps.setString(i+1, parameters[i]);
}
}
rs=ps.executeQuery();
if(rs.next())
return true;
else
return false;
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally{
}
// System.out.println(url+username+password);
}
public static void update(String sql, String[] parmeters) {
try {
ps = conn.prepareStatement(sql);
if (parmeters != null) {
for (int i = 0; i < parmeters.length; i++) {
ps.setString(i + 1, parmeters[i]);
}
}
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
long date = Date.parse("November 11, 2014");
System.out.println(new SimpleDateFormat().format("November 11, 2014"));
String sql="insert into android (ID,UpdatedTime,ContentRating,Description,RatingCount,AppSize,CategoryUrl,AppVersion) values ('com.hyperbeard.muertitos5','"+Date.parse("November 11, 2014")+"','Everyone',&