- 浏览: 1505303 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (798)
- struts2 (42)
- servlet (20)
- quartz (4)
- jquery & ajax (24)
- tomcat (5)
- javascript (15)
- struts1 (8)
- 搜索关键字及链接 (3)
- fckeditor (3)
- Apache (5)
- spring (22)
- linux (3)
- 企业应用 (8)
- 综合应用 (13)
- 服务器 (2)
- 数据库 (85)
- 性能调优 (21)
- 网络应用 (15)
- 缓存技术 (8)
- 设计模式 (39)
- 面试题 (7)
- 程序人生&前辈程序员 (29)
- java基础 (59)
- hibernate (75)
- log4j (4)
- http (11)
- 架构设计 (28)
- 网页设计 (12)
- java邮件 (4)
- 相关工具 (11)
- ognl (7)
- 工作笔记 (18)
- 知识面扩展 (12)
- oracle异常 (1)
- 正则表达式 (2)
- java异常 (5)
- 项目实践&管理 (1)
- 专业术语 (11)
- 网站参考 (1)
- 论坛话题 (2)
- web应用 (11)
- cxf&webservice (22)
- freemarker (3)
- 开源项目 (9)
- eos (1)
- ibatis (6)
- 自定义标签 (3)
- jsp (3)
- 内部非公开文档(注意:保存为草稿) (0)
- 国内外知名企业 (2)
- 网店 (3)
- 分页 (1)
- 消费者习惯 (2)
- 每日关注 (1)
- 商业信息 (18)
- 关注商业网站 (1)
- 生活常识 (3)
- 新闻 (2)
- xml&JSON (5)
- solaris (1)
- apache.common (3)
- BLOB/CLOB (1)
- lucene (2)
- JMS (14)
- 社会进程 (8)
- SSH扩展 (2)
- 消费心理 (1)
- 珠三角 (1)
- 设计文档 (1)
- XWork&webwork (1)
- 软件工程 (3)
- 数据库及链接 (1)
- RMI (2)
- 国内外知名企业&人物 (1)
最新评论
-
司c马:
简介易懂、
OutputStream和InputStream的区别 -
在世界的中心呼喚愛:
解决我的问题
Java获取客户端的真实IP地址 -
bo_hai:
都是些基本的概念呀!
SSO -
tian_4238:
哥们,你也是搞水利这块的吧。
巧用SQLQuery中的addScalar -
loveEVERYday:
java.util.Date、java.sql.Date、java.sql.Time、java.sql.Timestamp小结
package test;
/*
* Created on 2010-08-16
* 注意:执行之前,需记录tb_trouble_code_desc,tb_trouble_code记录数,最大的ID,另外将序列值调整到最大的ID
*
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Arrays;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
/**
* @author madp
* 用于抓取canobd2网站上故障码数据
*/
public class GrabDTCByHttpClient extends Thread{
private static final Logger log = Logger.getLogger(GrabDTCByHttpClient.class);
String [] makeArray = null;
public GrabDTCByHttpClient(){}
public GrabDTCByHttpClient(String [] makes){
makeArray = makes;
}
public static void main(String[] args) {
runMain();
}
public static void runMain(){
String [] makes = {
"Infiniti",
"Isuzu",
"Jaguar",
"Jeep",
"Kia",
"Land Rover",
"Lexus",
"Lincoln",
"Maybach",
"Mazda",
"Mercedes-Benz",
"Mercury",
"Merkur",
"Mitsubishi",
"Nissan",
"Oldsmobile",
"Plymouth",
"Pontiac",
"Porsche",
"Saab",
"Saturn",
"Scion",
"Sterling",
"Subaru",
"Suzuki",
"Toyota",
"Volkswagen"};
String [] bb = new String[1];
/** 利用多线程,开启27个线程 **/
for(int i=0;i<27;i++){
System.arraycopy(makes, i, bb, 0, 1);
for(int j=0;j<1;j++){
System.out.println("--bb-->"+bb[j]);
}
Thread thread = new GrabDTCByHttpClient(bb);
thread.start();
}
}
/**
* 的到Http请求结果
*
* @param url请求地址
* @param parms请求参数
* @return
*/
public byte[] getBody(String url, Map parms) {
byte[] body = null;
// 构造HttpClient的实例
HttpClient httpClient = new HttpClient();
// 创建post方法的实例
PostMethod postMethod = new PostMethod(url);
// 填入各个表单域的值
NameValuePair[] data = new NameValuePair[parms.keySet().size()];
Iterator it = parms.entrySet().iterator();
int i = 0;
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
data[i] = new NameValuePair(key.toString(), value.toString());
i++;
}
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
try {
// 执行postMethod
int statusCode = httpClient.executeMethod(postMethod); // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
// 301或者302
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
// 从头中取出转向的地址
Header locationHeader = postMethod
.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
System.out
.println("The page was redirected to:" + location);
} else {
System.err.println("Location field value is null.");
}
}
body = postMethod.getResponseBody();
} catch (Exception e) {
e.printStackTrace();
System.out.println("--getBody出现问题--");
log.info("--getBody出现问题--"+e);
runMain();
}
return body;
}
public void run(){
runGrab(makeArray);
}
public void runGrab(String [] makes){
synchronized(makes){
File file11 = new File("d://commonMakes.txt");
FileOutputStream fos11 = null;
try {
fos11 = new FileOutputStream(file11);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
PrintWriter pw11 = new PrintWriter(fos11);
String url = "http://www.canobd2.com/DtcLibrary/default.aspx";
/** 数组:所有车系 注意:Land Rover车系单独处理 别忘记了 **/
/** 如果执行中断,将当前的车系独立出来,将没有执行的故障码执行完后再列出以后的车系,并且将FOR循环里变量还原为初始状态,即从0-10迭代**/
// String[] makes = { /** 检查车系是否完整 **/
// "Hyundai" };
/**
"Acura",
"Alfa Romeo",
"Asuna",
"Audi",
"BMW",
"Buick",
"Cadillac",
"Chevrolet",
"Chrysler",
"Daewoo",
"Dodge",
"Eagle",
"Ford",
"Geo",
"GMC",
"Honda",
"Hyundai",
"Infiniti",
"Isuzu",
"Jaguar",
"Jeep",
"Kia",
"Land Rover",
"Lexus",
"Lincoln",
"Maybach",
"Mazda",
"Mercedes-Benz",
"Mercury",
"Merkur",
"Mitsubishi",
"Nissan",
"Oldsmobile",
"Plymouth",
"Pontiac",
"Porsche",
"Saab",
"Saturn",
"Scion",
"Sterling",
"Subaru",
"Suzuki",
"Toyota",
"Volkswagen"
*/
/** 将Land Rover车系单独出来处理,因为与crecorder的车系名称LANDROVER有区别 **/
Long startTime = new Date().getTime();
/** troublecodes:通用、专用故障码 **/
List<String> troublecodes = new ArrayList<String>();
/** finalTroublecodes:需抓取数据的故障码 **/
List<String> finalTroublecodes = new ArrayList<String>();
String strP0 = null;
String strP1 = null;
String strP2 = null;
String strP3 = null;
String str1 = null;
for(int i=1;i<10;i++){
for(int j=0;j<10;j++){
for(int k=0;k<10;k++){
str1 = String.valueOf(i)+String.valueOf(j)+String.valueOf(k);
strP0 = "P0"+ str1;
strP2 = "P2"+ str1;
if(Integer.parseInt(str1.substring(0, 1))>=4){
strP3 = "P3"+ str1;
}
if(strP0!=null){
troublecodes.add(strP0);
}
if(strP2!=null){
troublecodes.add(strP2);
}
if(strP3!=null){
troublecodes.add(strP3);
}
}
}
}
/** 获得数据库连接 **/
Connection connection = null;
try {
connection = getConnection();
} catch (ClassNotFoundException e3) {
e3.printStackTrace();
System.out.println("--getConnection出现问题--");
new GrabDTCByHttpClient().start();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("--getConnection出现问题--");
new GrabDTCByHttpClient().start();
}
File file1 = new File("d://commonRecord.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file1);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
PrintWriter pw = new PrintWriter(fos);
/** 分为两种情况 **/
/** 1、通用情况 **/
String commonTroubleCodeSql = "select count(1) from tb_trouble_code_desc where trouble_code = ?" +
" and lan_id = 15";
Long count = null;
System.out.println("---通用故障码总数量-->"+troublecodes.size());
print(pw,"---通用故障码总数量-->"+troublecodes.size());
for(int i=0;i<troublecodes.size();i++){
/** 查看数据库中是否有记录 **/
count = getCount(connection,troublecodes.get(i),null,commonTroubleCodeSql);
String str = "--通用:数据库中记录数-->"+count+"->"+troublecodes.get(i);
System.out.println(str);
print(pw,str);
if(count==0){ /** 没有记录时,收集故障码 **/
System.out.println("--通用:待插入的故障码-->"+troublecodes.get(i));
print(pw,"--通用:待插入的故障码-->"+troublecodes.get(i));
finalTroublecodes.add(troublecodes.get(i));
}
}
System.out.println("---通用:待抓取并插入的故障码总数量-->"+finalTroublecodes.size()); /** 此数据应该与共插入的条数匹配 **/
print(pw,"---通用:待抓取并插入的故障码总数量-->"+finalTroublecodes.size());
/** 通用情况,所有车系对应统一的故障码描述,所以只取一个车系**/
String subMake [] = new String[1];
System.arraycopy(makes, 0, subMake, 0, 1);
for(String sr:makes){
print(pw11, sr);/** 打印每个开启的线程里的车系 **/
}
for(int i=0;i<subMake.length;i++){
System.out.println(subMake[i]);
}
grabAndInsert(pw,subMake,finalTroublecodes,url,connection);
print(pw,"--通用结束,开始专用情况--");
if(pw!=null){
pw.close();
pw = null;
}
try{
if(fos!=null){
fos.close();
fos = null;
}
}catch(Exception e){
e.printStackTrace();
}
if(pw11!=null){
pw11.close();
pw11 = null;
}
try{
if(fos11!=null){
fos11.close();
fos11 = null;
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println("--通用结束,开始专用情况--");
System.out.println("************************");
/** 2、专用故障码情况 **/
troublecodes.clear();/** 清空 **/
finalTroublecodes.clear();/** 清空 **/
for(int i=1;i<10;i++){
for(int j=0;j<10;j++){
for(int k=0;k<10;k++){
str1 = String.valueOf(i)+String.valueOf(j)+String.valueOf(k);
strP1 = "P1"+ str1;
if(Integer.parseInt(str1.substring(0, 1))<4&&Integer.parseInt(str1.substring(0, 1))>=0){
strP3 = "P3"+ str1;
}
if(strP1!=null){
troublecodes.add(strP1);
}
if(strP3!=null){
troublecodes.add(strP3);
}
}
}
}
File file2 = new File("d://singleRecord.txt");
FileOutputStream fos2 = null;
try {
fos2 = new FileOutputStream(file2);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintWriter pw2 = new PrintWriter(fos2);
System.out.println("---专用故障码总数量-->"+troublecodes.size());
print(pw2,"---专用故障码总数量-->"+troublecodes.size());
String isExistSql = "select count(1) from tb_trouble_code_desc where trouble_code = ?" +
" and lan_id = 15 and models = ?";
for(int j=0;j<makes.length;j++){
for(int i=0;i<troublecodes.size();i++){
count = getCount(connection,troublecodes.get(i),makes[j],isExistSql);
System.out.println("--专用:数据库中记录数-->"+count);
print(pw2,"--专用:数据库中记录数-->"+count);
if(count==0){/** 数据库中无记录,列出有哪些troublecode,然后统一抓取后插入数据库 **/
System.out.println("--专用:待插入的故障码-->"+troublecodes.get(i)+"->"+makes[j]);
print(pw2,"--专用:待插入的故障码-->"+troublecodes.get(i)+"->"+makes[j]);
finalTroublecodes.add(troublecodes.get(i));
}
}
}
System.out.println("---专用:待抓取并插入的故障码总数量-->"+finalTroublecodes.size()); /** 此数据应该与共插入的条数匹配 **/
print(pw2,"---专用:待抓取并插入的故障码总数量-->"+finalTroublecodes.size());
grabAndInsert(pw2,makes,finalTroublecodes,url,connection);
if(pw2!=null){
pw2.close();
pw2 = null;
}
try{
if(fos2!=null){
fos2.close();
fos2 = null;
}
}catch(Exception e){
e.printStackTrace();
}
/** 关闭数据库链接 **/
closeConnection(connection);
Long endTime = new Date().getTime();
System.out.println("---------success----->"+(endTime-startTime)); /** 抓取数据及插入数据库,执行成功 **/
}
}
public static Long getCount(Connection connection,String troubleCode,String model,String sql){ /** 获取记录数 **/
PreparedStatement pr = null;
ResultSet rs = null;
Long count = null;
try {
pr = connection.prepareStatement(sql);
if(model==null){/** 判断通用故障码 如:P0开头等**/
pr.setString(1, troubleCode);
}else{
pr.setString(1, troubleCode);
pr.setString(2, model);
}
rs = pr.executeQuery();
if(rs.next()){
count = rs.getLong(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(rs!=null){
rs.close();
rs = null;
}
if(pr!=null){
pr.close();
pr = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return count;
}
/** 打印输出到文件 **/
public void print(PrintWriter pw,String str){
try{
pw.println(str);
pw.flush();
}catch(Exception e){
e.printStackTrace();
}
}
/** 抓取数据后插入数据库 **/
public void grabAndInsert(PrintWriter pw,String[] makes,List<String> troublecodes,String url,Connection connection){
Map parms1 = new HashMap();
GrabDTCByHttpClient client = new GrabDTCByHttpClient();
parms1.put("submitButton", "Submit");
parms1.put("__EVENTARGUMENT", "");
parms1.put("__EVENTTARGET", "");
parms1.put("__sx", "0");
parms1
.put(
"__VIEWSTATE",
"/wEPDwULLTE5MzcxNjE1MzkPFgQeDl9fUHJldmlvdXNQYWdlZR4LX19SZXR1cm5VcmwFGC9EdGNMaWJyYXJ5L2RlZmF1bHQuYXNweBYCAgEPZBYIZg9kFgZmDxYCHgVzdHlsZQUNZGlzcGxheTpub25lO2QCAg8WAh4HVmlzaWJsZWdkAgMPFgIfA2hkAgUPEA8WAh4LXyFEYXRhQm91bmRnZBAVLQ5TZWxlY3QgTWFrZS4uLgVBY3VyYQpBbGZhIFJvbWVvBUFzdW5hBEF1ZGkDQk1XBUJ1aWNrCENhZGlsbGFjCUNoZXZyb2xldAhDaHJ5c2xlcgZEYWV3b28FRG9kZ2UFRWFnbGUERm9yZANHZW8DR01DBUhvbmRhB0h5dW5kYWkISW5maW5pdGkFSXN1enUGSmFndWFyBEplZXADS2lhCkxhbmQgUm92ZXIFTGV4dXMHTGluY29sbgdNYXliYWNoBU1hemRhDU1lcmNlZGVzLUJlbnoHTWVyY3VyeQZNZXJrdXIKTWl0c3ViaXNoaQZOaXNzYW4KT2xkc21vYmlsZQhQbHltb3V0aAdQb250aWFjB1BvcnNjaGUEU2FhYgZTYXR1cm4FU2Npb24IU3RlcmxpbmcGU3ViYXJ1BlN1enVraQZUb3lvdGEKVm9sa3N3YWdlbhUtAAVBY3VyYQpBbGZhIFJvbWVvBUFzdW5hBEF1ZGkDQk1XBUJ1aWNrCENhZGlsbGFjCUNoZXZyb2xldAhDaHJ5c2xlcgZEYWV3b28FRG9kZ2UFRWFnbGUERm9yZANHZW8DR01DBUhvbmRhB0h5dW5kYWkISW5maW5pdGkFSXN1enUGSmFndWFyBEplZXADS2lhCkxhbmQgUm92ZXIFTGV4dXMHTGluY29sbgdNYXliYWNoBU1hemRhDU1lcmNlZGVzLUJlbnoHTWVyY3VyeQZNZXJrdXIKTWl0c3ViaXNoaQZOaXNzYW4KT2xkc21vYmlsZQhQbHltb3V0aAdQb250aWFjB1BvcnNjaGUEU2FhYgZTYXR1cm4FU2Npb24IU3RlcmxpbmcGU3ViYXJ1BlN1enVraQZUb3lvdGEKVm9sa3N3YWdlbhQrAy1nZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dkZAIIDxYCHwNnFgZmDw8WAh4EVGV4dAUFUDAwMDFkZAIBDw8WAh8FBQNCTVdkZAICDw8WAh8FBSxGdWVsIFZvbHVtZSBSZWd1bGF0b3IgQ29udHJvbCBDaXJjdWl0IC8gT3BlbmRkAgkPFgIfA2hkZOkD5Ngxd3cE1BXKSzs9IYasdy+1");
parms1.put("__sy", "102");
int ii = 0;
int jj = 0;
for (int i = 0; i < makes.length; i++) { //45
parms1.put("vehicleMake", makes[i]);
for (int j = 0; j < troublecodes.size(); j++) { //4000
parms1.put("dtc", troublecodes.get(j));
// 根据车系、故障码查出描述
String result2 = null;
try {
result2 = new String(client.getBody(url, parms1),"utf-8");
} catch (UnsupportedEncodingException e2) {
System.out.println("-- 字符串转换错误 --");
e2.printStackTrace();
}
//String [] str = result2.split("descriptionLabel"+"\\p{Punct}\\p{Punct}"); /** 判断返回的字符串中是否包含descriptionLabel"> **/
String regex = "<span id=\"descriptionLabel\">([^<]*)</span>";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(result2);
if(matcher.find()){ /** 若length>1,一定包含 **/
String str = matcher.group(1);
/**
* 获得的数据:
* 车系:makes[i]
* 故障码:troublecodes.get(j)
* 描述:str[1]
*/
if(makes[i] == "Land Rover"){
makes[i] = "LANDROVER";
}
System.out.println("-- 插入的数据 -->"+makes[i].toUpperCase()+" -> "+troublecodes.get(j)+" -> "+str);
print(pw,"-- 插入的数据 -->"+makes[i].toUpperCase()+" -> "+troublecodes.get(j)+" -> "+str);
preInsertData(connection,makes[i].toUpperCase(),troublecodes.get(j),str);
ii++;
}else{ /** 没有找到对应的数据 **/
System.out.println("--没有抓取到数据-->"+makes[i]+"->"+troublecodes.get(j));
print(pw, "--没有抓取到数据-->"+makes[i]+"->"+troublecodes.get(j));
jj++;
//System.out.println("-------------***--str--22-----"+makes[i]+"@"+troublecodes.get(j)+"@"+"Sorry, we were unable to find that code.");
//String ret = makes[i]+"@"+troublecodes.get(j)+"@"+"Sorry, we were unable to find that code.";
//pw.println(ret);
//pw.flush();
}
}
}
System.out.println("--共插入数据库的数据量-->"+ii);
System.out.println("--没有抓取到数据的总量-->"+jj);
print(pw, "--共插入数据库的数据量-->"+ii);
print(pw, "--没有抓取到数据的总量-->"+jj);
}
/** 获得连接 *
* @throws ClassNotFoundException
* @throws SQLException */
public static Connection getConnection() throws ClassNotFoundException, SQLException{
String driverName = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.17.96:1521:TX431";
String user = "crecorder";
String password = "crecorder";
Connection conn = null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url, user, password);
System.out.println("-- 获取数据库链接成功 --");
}catch(ClassNotFoundException e){
System.out.println("-- 获取数据库链接失败,驱动不存在 --");
e.printStackTrace();
throw e;
}catch (SQLException e) {
System.out.println("-- 获取数据库链接失败 --");
e.printStackTrace();
throw e;
}
return conn;
}
/** 准备插入数据 **/
public static void preInsertData(Connection connection,String make,String troubleCode,String desc){
System.out.println("-- 开始插入数据 --");
/** 根据故障码,获得父键PID **/
Long pid = getPidByTroubleCode(connection,troubleCode);
if(pid!=null){ /** 父键存在 **/
insertData(connection,pid,troubleCode,desc,make);
}else{ /** 父键不存在,插入故障码到父表,获得父键PID **/
pid = getPidByInsertTroubleCode(connection,troubleCode);
insertData(connection,pid,troubleCode,desc,make);
}
}
/** 插入数据 **/
public static void insertData(Connection connection,Long pid,String troubleCode,String desc,String make){
String sql = "insert into tb_trouble_code_desc values(trouble_code_desc_seq.nextval,?,?,'','',15,?,?,'','')";
// String string = "insert into tb_trouble_code_desc values(trouble_code_desc_seq.nextval,"+pid+","+troubleCode+",'','',15,"+desc+","+make+",'','')";
// System.out.println("-->"+string);
/**
* ID NUMBER(15)
PID NUMBER(15)
TROUBLE_CODE VARCHAR2(512)
DESCRIPTION VARCHAR2(1536)
PDF_PATH VARCHAR2(250)
LAN_ID NUMBER(15)
BRIEF_DESC VARCHAR2(1024)
MODELS VARCHAR2(256)
YEAR NUMBER(4)
TYPE NUMBER(1)
*/
PreparedStatement pstmt = null;
try {
pstmt = connection.prepareStatement(sql);
pstmt.setLong(1, pid);
pstmt.setString(2, troubleCode);
pstmt.setString(3, desc);
pstmt.setString(4, make);
pstmt.execute();
System.out.println("-- 插入数据成功 --");
} catch (SQLException e) {
System.out.println("-- 插入数据失败 --");
e.printStackTrace();
}
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/** 通过故障码查询父键PID**/
public static Long getPidByTroubleCode(Connection connection,String troubleCode){
Long pid = null;
/** 查询数据库,获得父键PID**/
String pidSql = "select id from tb_trouble_code t where t.trouble_code = ?";
ResultSet rs = null;
PreparedStatement pStatement = null;
try{
pStatement = connection.prepareStatement(pidSql);
pStatement.setString(1, troubleCode);
rs = pStatement.executeQuery();
while(rs.next()){
pid = rs.getLong("id"); /** 一条记录 **/
}
//System.out.println("-- 获取父键PID成功 -->"+pid);
}catch(SQLException e){
System.out.println("-- 获取父键PID失败 --");
e.printStackTrace();
}
try {
rs.close();
pStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return pid;
}
/** 插入故障码并返回主键PID**/
public static Long getPidByInsertTroubleCode(Connection connection,String troubleCode){
Long pid = null;
/** 为了获得主表的PID,先取sequence,再往主表插入记录。**/
String sequenceSql = "select trouble_code_seq.nextval pid from dual ";
try {
PreparedStatement pr = connection.prepareStatement(sequenceSql);
ResultSet rs = pr.executeQuery();
if(rs.next()){
pid = rs.getLong("pid");
}
System.out.println("-- 获取sequence,返回主键PID成功-->"+pid);
rs.close();
pr.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String troubleCodeSql = "insert into tb_trouble_code values(?,?,'','madp')";
/**
* ID NUMBER(15)
TROUBLE_CODE VARCHAR2(512)
ENTRY_TIME DATE
CREATOR VARCHAR2(60)
*/
try {
PreparedStatement prstmt = connection.prepareStatement(troubleCodeSql, Statement.RETURN_GENERATED_KEYS);
prstmt.setLong(1, pid);
prstmt.setString(2, troubleCode);
prstmt.execute();
// ResultSet rSet = prstmt.getGeneratedKeys();
// while(rSet.next()){
// System.out.println("----"+rSet.getString(1));
// //pid = Integer.valueOf(rSet.getInt(1)).longValue();
// }
System.out.println("-- 插入故障码成功--");
//rSet.close();
prstmt.close();
} catch (SQLException e) {
System.out.println("-- 插入故障码并返回主键PID失败--");
e.printStackTrace();
}
return pid;
}
/** 关闭数据库连接 **/
public static void closeConnection(Connection conn){
try{
if(!conn.isClosed()){
conn.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
/** 插入数据库中没有的车系 表:tb_car_desc *
* @throws ClassNotFoundException
* @throws SQLException */
public static void insertMake(String make) throws ClassNotFoundException, SQLException{
Connection connection = getConnection();
String sql = "select count(1) from tb_car_desc where code = ? and lan_id = 15 ";
try {
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, make);
ResultSet rs = ps.executeQuery();
Long count = null;
while(rs.next()){
count = rs.getLong(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
-- 插入数据成功 --
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at org.apache.commons.httpclient.ContentLengthInputStream.read(ContentLengthInputStream.java:169)
at java.io.FilterInputStream.read(FilterInputStream.java:116)
at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:107)
at java.io.FilterInputStream.read(FilterInputStream.java:90)
at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:126)
at org.apache.commons.httpclient.HttpMethodBase.getResponseBody(HttpMethodBase.java:684)
at test.GrabDTCByHttpClient.getBody(GrabDTCByHttpClient.java:86)
at test.GrabDTCByHttpClient.main(GrabDTCByHttpClient.java:247)
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.<init>(String.java:515)
at test.GrabDTCByHttpClient.main(GrabDTCByHttpClient.java:247)
**/
/*
* Created on 2010-08-16
* 注意:执行之前,需记录tb_trouble_code_desc,tb_trouble_code记录数,最大的ID,另外将序列值调整到最大的ID
*
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Arrays;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
/**
* @author madp
* 用于抓取canobd2网站上故障码数据
*/
public class GrabDTCByHttpClient extends Thread{
private static final Logger log = Logger.getLogger(GrabDTCByHttpClient.class);
String [] makeArray = null;
public GrabDTCByHttpClient(){}
public GrabDTCByHttpClient(String [] makes){
makeArray = makes;
}
public static void main(String[] args) {
runMain();
}
public static void runMain(){
String [] makes = {
"Infiniti",
"Isuzu",
"Jaguar",
"Jeep",
"Kia",
"Land Rover",
"Lexus",
"Lincoln",
"Maybach",
"Mazda",
"Mercedes-Benz",
"Mercury",
"Merkur",
"Mitsubishi",
"Nissan",
"Oldsmobile",
"Plymouth",
"Pontiac",
"Porsche",
"Saab",
"Saturn",
"Scion",
"Sterling",
"Subaru",
"Suzuki",
"Toyota",
"Volkswagen"};
String [] bb = new String[1];
/** 利用多线程,开启27个线程 **/
for(int i=0;i<27;i++){
System.arraycopy(makes, i, bb, 0, 1);
for(int j=0;j<1;j++){
System.out.println("--bb-->"+bb[j]);
}
Thread thread = new GrabDTCByHttpClient(bb);
thread.start();
}
}
/**
* 的到Http请求结果
*
* @param url请求地址
* @param parms请求参数
* @return
*/
public byte[] getBody(String url, Map parms) {
byte[] body = null;
// 构造HttpClient的实例
HttpClient httpClient = new HttpClient();
// 创建post方法的实例
PostMethod postMethod = new PostMethod(url);
// 填入各个表单域的值
NameValuePair[] data = new NameValuePair[parms.keySet().size()];
Iterator it = parms.entrySet().iterator();
int i = 0;
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
data[i] = new NameValuePair(key.toString(), value.toString());
i++;
}
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
try {
// 执行postMethod
int statusCode = httpClient.executeMethod(postMethod); // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
// 301或者302
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
// 从头中取出转向的地址
Header locationHeader = postMethod
.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
System.out
.println("The page was redirected to:" + location);
} else {
System.err.println("Location field value is null.");
}
}
body = postMethod.getResponseBody();
} catch (Exception e) {
e.printStackTrace();
System.out.println("--getBody出现问题--");
log.info("--getBody出现问题--"+e);
runMain();
}
return body;
}
public void run(){
runGrab(makeArray);
}
public void runGrab(String [] makes){
synchronized(makes){
File file11 = new File("d://commonMakes.txt");
FileOutputStream fos11 = null;
try {
fos11 = new FileOutputStream(file11);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
PrintWriter pw11 = new PrintWriter(fos11);
String url = "http://www.canobd2.com/DtcLibrary/default.aspx";
/** 数组:所有车系 注意:Land Rover车系单独处理 别忘记了 **/
/** 如果执行中断,将当前的车系独立出来,将没有执行的故障码执行完后再列出以后的车系,并且将FOR循环里变量还原为初始状态,即从0-10迭代**/
// String[] makes = { /** 检查车系是否完整 **/
// "Hyundai" };
/**
"Acura",
"Alfa Romeo",
"Asuna",
"Audi",
"BMW",
"Buick",
"Cadillac",
"Chevrolet",
"Chrysler",
"Daewoo",
"Dodge",
"Eagle",
"Ford",
"Geo",
"GMC",
"Honda",
"Hyundai",
"Infiniti",
"Isuzu",
"Jaguar",
"Jeep",
"Kia",
"Land Rover",
"Lexus",
"Lincoln",
"Maybach",
"Mazda",
"Mercedes-Benz",
"Mercury",
"Merkur",
"Mitsubishi",
"Nissan",
"Oldsmobile",
"Plymouth",
"Pontiac",
"Porsche",
"Saab",
"Saturn",
"Scion",
"Sterling",
"Subaru",
"Suzuki",
"Toyota",
"Volkswagen"
*/
/** 将Land Rover车系单独出来处理,因为与crecorder的车系名称LANDROVER有区别 **/
Long startTime = new Date().getTime();
/** troublecodes:通用、专用故障码 **/
List<String> troublecodes = new ArrayList<String>();
/** finalTroublecodes:需抓取数据的故障码 **/
List<String> finalTroublecodes = new ArrayList<String>();
String strP0 = null;
String strP1 = null;
String strP2 = null;
String strP3 = null;
String str1 = null;
for(int i=1;i<10;i++){
for(int j=0;j<10;j++){
for(int k=0;k<10;k++){
str1 = String.valueOf(i)+String.valueOf(j)+String.valueOf(k);
strP0 = "P0"+ str1;
strP2 = "P2"+ str1;
if(Integer.parseInt(str1.substring(0, 1))>=4){
strP3 = "P3"+ str1;
}
if(strP0!=null){
troublecodes.add(strP0);
}
if(strP2!=null){
troublecodes.add(strP2);
}
if(strP3!=null){
troublecodes.add(strP3);
}
}
}
}
/** 获得数据库连接 **/
Connection connection = null;
try {
connection = getConnection();
} catch (ClassNotFoundException e3) {
e3.printStackTrace();
System.out.println("--getConnection出现问题--");
new GrabDTCByHttpClient().start();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("--getConnection出现问题--");
new GrabDTCByHttpClient().start();
}
File file1 = new File("d://commonRecord.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file1);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
PrintWriter pw = new PrintWriter(fos);
/** 分为两种情况 **/
/** 1、通用情况 **/
String commonTroubleCodeSql = "select count(1) from tb_trouble_code_desc where trouble_code = ?" +
" and lan_id = 15";
Long count = null;
System.out.println("---通用故障码总数量-->"+troublecodes.size());
print(pw,"---通用故障码总数量-->"+troublecodes.size());
for(int i=0;i<troublecodes.size();i++){
/** 查看数据库中是否有记录 **/
count = getCount(connection,troublecodes.get(i),null,commonTroubleCodeSql);
String str = "--通用:数据库中记录数-->"+count+"->"+troublecodes.get(i);
System.out.println(str);
print(pw,str);
if(count==0){ /** 没有记录时,收集故障码 **/
System.out.println("--通用:待插入的故障码-->"+troublecodes.get(i));
print(pw,"--通用:待插入的故障码-->"+troublecodes.get(i));
finalTroublecodes.add(troublecodes.get(i));
}
}
System.out.println("---通用:待抓取并插入的故障码总数量-->"+finalTroublecodes.size()); /** 此数据应该与共插入的条数匹配 **/
print(pw,"---通用:待抓取并插入的故障码总数量-->"+finalTroublecodes.size());
/** 通用情况,所有车系对应统一的故障码描述,所以只取一个车系**/
String subMake [] = new String[1];
System.arraycopy(makes, 0, subMake, 0, 1);
for(String sr:makes){
print(pw11, sr);/** 打印每个开启的线程里的车系 **/
}
for(int i=0;i<subMake.length;i++){
System.out.println(subMake[i]);
}
grabAndInsert(pw,subMake,finalTroublecodes,url,connection);
print(pw,"--通用结束,开始专用情况--");
if(pw!=null){
pw.close();
pw = null;
}
try{
if(fos!=null){
fos.close();
fos = null;
}
}catch(Exception e){
e.printStackTrace();
}
if(pw11!=null){
pw11.close();
pw11 = null;
}
try{
if(fos11!=null){
fos11.close();
fos11 = null;
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println("--通用结束,开始专用情况--");
System.out.println("************************");
/** 2、专用故障码情况 **/
troublecodes.clear();/** 清空 **/
finalTroublecodes.clear();/** 清空 **/
for(int i=1;i<10;i++){
for(int j=0;j<10;j++){
for(int k=0;k<10;k++){
str1 = String.valueOf(i)+String.valueOf(j)+String.valueOf(k);
strP1 = "P1"+ str1;
if(Integer.parseInt(str1.substring(0, 1))<4&&Integer.parseInt(str1.substring(0, 1))>=0){
strP3 = "P3"+ str1;
}
if(strP1!=null){
troublecodes.add(strP1);
}
if(strP3!=null){
troublecodes.add(strP3);
}
}
}
}
File file2 = new File("d://singleRecord.txt");
FileOutputStream fos2 = null;
try {
fos2 = new FileOutputStream(file2);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintWriter pw2 = new PrintWriter(fos2);
System.out.println("---专用故障码总数量-->"+troublecodes.size());
print(pw2,"---专用故障码总数量-->"+troublecodes.size());
String isExistSql = "select count(1) from tb_trouble_code_desc where trouble_code = ?" +
" and lan_id = 15 and models = ?";
for(int j=0;j<makes.length;j++){
for(int i=0;i<troublecodes.size();i++){
count = getCount(connection,troublecodes.get(i),makes[j],isExistSql);
System.out.println("--专用:数据库中记录数-->"+count);
print(pw2,"--专用:数据库中记录数-->"+count);
if(count==0){/** 数据库中无记录,列出有哪些troublecode,然后统一抓取后插入数据库 **/
System.out.println("--专用:待插入的故障码-->"+troublecodes.get(i)+"->"+makes[j]);
print(pw2,"--专用:待插入的故障码-->"+troublecodes.get(i)+"->"+makes[j]);
finalTroublecodes.add(troublecodes.get(i));
}
}
}
System.out.println("---专用:待抓取并插入的故障码总数量-->"+finalTroublecodes.size()); /** 此数据应该与共插入的条数匹配 **/
print(pw2,"---专用:待抓取并插入的故障码总数量-->"+finalTroublecodes.size());
grabAndInsert(pw2,makes,finalTroublecodes,url,connection);
if(pw2!=null){
pw2.close();
pw2 = null;
}
try{
if(fos2!=null){
fos2.close();
fos2 = null;
}
}catch(Exception e){
e.printStackTrace();
}
/** 关闭数据库链接 **/
closeConnection(connection);
Long endTime = new Date().getTime();
System.out.println("---------success----->"+(endTime-startTime)); /** 抓取数据及插入数据库,执行成功 **/
}
}
public static Long getCount(Connection connection,String troubleCode,String model,String sql){ /** 获取记录数 **/
PreparedStatement pr = null;
ResultSet rs = null;
Long count = null;
try {
pr = connection.prepareStatement(sql);
if(model==null){/** 判断通用故障码 如:P0开头等**/
pr.setString(1, troubleCode);
}else{
pr.setString(1, troubleCode);
pr.setString(2, model);
}
rs = pr.executeQuery();
if(rs.next()){
count = rs.getLong(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(rs!=null){
rs.close();
rs = null;
}
if(pr!=null){
pr.close();
pr = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return count;
}
/** 打印输出到文件 **/
public void print(PrintWriter pw,String str){
try{
pw.println(str);
pw.flush();
}catch(Exception e){
e.printStackTrace();
}
}
/** 抓取数据后插入数据库 **/
public void grabAndInsert(PrintWriter pw,String[] makes,List<String> troublecodes,String url,Connection connection){
Map parms1 = new HashMap();
GrabDTCByHttpClient client = new GrabDTCByHttpClient();
parms1.put("submitButton", "Submit");
parms1.put("__EVENTARGUMENT", "");
parms1.put("__EVENTTARGET", "");
parms1.put("__sx", "0");
parms1
.put(
"__VIEWSTATE",
"/wEPDwULLTE5MzcxNjE1MzkPFgQeDl9fUHJldmlvdXNQYWdlZR4LX19SZXR1cm5VcmwFGC9EdGNMaWJyYXJ5L2RlZmF1bHQuYXNweBYCAgEPZBYIZg9kFgZmDxYCHgVzdHlsZQUNZGlzcGxheTpub25lO2QCAg8WAh4HVmlzaWJsZWdkAgMPFgIfA2hkAgUPEA8WAh4LXyFEYXRhQm91bmRnZBAVLQ5TZWxlY3QgTWFrZS4uLgVBY3VyYQpBbGZhIFJvbWVvBUFzdW5hBEF1ZGkDQk1XBUJ1aWNrCENhZGlsbGFjCUNoZXZyb2xldAhDaHJ5c2xlcgZEYWV3b28FRG9kZ2UFRWFnbGUERm9yZANHZW8DR01DBUhvbmRhB0h5dW5kYWkISW5maW5pdGkFSXN1enUGSmFndWFyBEplZXADS2lhCkxhbmQgUm92ZXIFTGV4dXMHTGluY29sbgdNYXliYWNoBU1hemRhDU1lcmNlZGVzLUJlbnoHTWVyY3VyeQZNZXJrdXIKTWl0c3ViaXNoaQZOaXNzYW4KT2xkc21vYmlsZQhQbHltb3V0aAdQb250aWFjB1BvcnNjaGUEU2FhYgZTYXR1cm4FU2Npb24IU3RlcmxpbmcGU3ViYXJ1BlN1enVraQZUb3lvdGEKVm9sa3N3YWdlbhUtAAVBY3VyYQpBbGZhIFJvbWVvBUFzdW5hBEF1ZGkDQk1XBUJ1aWNrCENhZGlsbGFjCUNoZXZyb2xldAhDaHJ5c2xlcgZEYWV3b28FRG9kZ2UFRWFnbGUERm9yZANHZW8DR01DBUhvbmRhB0h5dW5kYWkISW5maW5pdGkFSXN1enUGSmFndWFyBEplZXADS2lhCkxhbmQgUm92ZXIFTGV4dXMHTGluY29sbgdNYXliYWNoBU1hemRhDU1lcmNlZGVzLUJlbnoHTWVyY3VyeQZNZXJrdXIKTWl0c3ViaXNoaQZOaXNzYW4KT2xkc21vYmlsZQhQbHltb3V0aAdQb250aWFjB1BvcnNjaGUEU2FhYgZTYXR1cm4FU2Npb24IU3RlcmxpbmcGU3ViYXJ1BlN1enVraQZUb3lvdGEKVm9sa3N3YWdlbhQrAy1nZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dkZAIIDxYCHwNnFgZmDw8WAh4EVGV4dAUFUDAwMDFkZAIBDw8WAh8FBQNCTVdkZAICDw8WAh8FBSxGdWVsIFZvbHVtZSBSZWd1bGF0b3IgQ29udHJvbCBDaXJjdWl0IC8gT3BlbmRkAgkPFgIfA2hkZOkD5Ngxd3cE1BXKSzs9IYasdy+1");
parms1.put("__sy", "102");
int ii = 0;
int jj = 0;
for (int i = 0; i < makes.length; i++) { //45
parms1.put("vehicleMake", makes[i]);
for (int j = 0; j < troublecodes.size(); j++) { //4000
parms1.put("dtc", troublecodes.get(j));
// 根据车系、故障码查出描述
String result2 = null;
try {
result2 = new String(client.getBody(url, parms1),"utf-8");
} catch (UnsupportedEncodingException e2) {
System.out.println("-- 字符串转换错误 --");
e2.printStackTrace();
}
//String [] str = result2.split("descriptionLabel"+"\\p{Punct}\\p{Punct}"); /** 判断返回的字符串中是否包含descriptionLabel"> **/
String regex = "<span id=\"descriptionLabel\">([^<]*)</span>";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(result2);
if(matcher.find()){ /** 若length>1,一定包含 **/
String str = matcher.group(1);
/**
* 获得的数据:
* 车系:makes[i]
* 故障码:troublecodes.get(j)
* 描述:str[1]
*/
if(makes[i] == "Land Rover"){
makes[i] = "LANDROVER";
}
System.out.println("-- 插入的数据 -->"+makes[i].toUpperCase()+" -> "+troublecodes.get(j)+" -> "+str);
print(pw,"-- 插入的数据 -->"+makes[i].toUpperCase()+" -> "+troublecodes.get(j)+" -> "+str);
preInsertData(connection,makes[i].toUpperCase(),troublecodes.get(j),str);
ii++;
}else{ /** 没有找到对应的数据 **/
System.out.println("--没有抓取到数据-->"+makes[i]+"->"+troublecodes.get(j));
print(pw, "--没有抓取到数据-->"+makes[i]+"->"+troublecodes.get(j));
jj++;
//System.out.println("-------------***--str--22-----"+makes[i]+"@"+troublecodes.get(j)+"@"+"Sorry, we were unable to find that code.");
//String ret = makes[i]+"@"+troublecodes.get(j)+"@"+"Sorry, we were unable to find that code.";
//pw.println(ret);
//pw.flush();
}
}
}
System.out.println("--共插入数据库的数据量-->"+ii);
System.out.println("--没有抓取到数据的总量-->"+jj);
print(pw, "--共插入数据库的数据量-->"+ii);
print(pw, "--没有抓取到数据的总量-->"+jj);
}
/** 获得连接 *
* @throws ClassNotFoundException
* @throws SQLException */
public static Connection getConnection() throws ClassNotFoundException, SQLException{
String driverName = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.17.96:1521:TX431";
String user = "crecorder";
String password = "crecorder";
Connection conn = null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url, user, password);
System.out.println("-- 获取数据库链接成功 --");
}catch(ClassNotFoundException e){
System.out.println("-- 获取数据库链接失败,驱动不存在 --");
e.printStackTrace();
throw e;
}catch (SQLException e) {
System.out.println("-- 获取数据库链接失败 --");
e.printStackTrace();
throw e;
}
return conn;
}
/** 准备插入数据 **/
public static void preInsertData(Connection connection,String make,String troubleCode,String desc){
System.out.println("-- 开始插入数据 --");
/** 根据故障码,获得父键PID **/
Long pid = getPidByTroubleCode(connection,troubleCode);
if(pid!=null){ /** 父键存在 **/
insertData(connection,pid,troubleCode,desc,make);
}else{ /** 父键不存在,插入故障码到父表,获得父键PID **/
pid = getPidByInsertTroubleCode(connection,troubleCode);
insertData(connection,pid,troubleCode,desc,make);
}
}
/** 插入数据 **/
public static void insertData(Connection connection,Long pid,String troubleCode,String desc,String make){
String sql = "insert into tb_trouble_code_desc values(trouble_code_desc_seq.nextval,?,?,'','',15,?,?,'','')";
// String string = "insert into tb_trouble_code_desc values(trouble_code_desc_seq.nextval,"+pid+","+troubleCode+",'','',15,"+desc+","+make+",'','')";
// System.out.println("-->"+string);
/**
* ID NUMBER(15)
PID NUMBER(15)
TROUBLE_CODE VARCHAR2(512)
DESCRIPTION VARCHAR2(1536)
PDF_PATH VARCHAR2(250)
LAN_ID NUMBER(15)
BRIEF_DESC VARCHAR2(1024)
MODELS VARCHAR2(256)
YEAR NUMBER(4)
TYPE NUMBER(1)
*/
PreparedStatement pstmt = null;
try {
pstmt = connection.prepareStatement(sql);
pstmt.setLong(1, pid);
pstmt.setString(2, troubleCode);
pstmt.setString(3, desc);
pstmt.setString(4, make);
pstmt.execute();
System.out.println("-- 插入数据成功 --");
} catch (SQLException e) {
System.out.println("-- 插入数据失败 --");
e.printStackTrace();
}
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/** 通过故障码查询父键PID**/
public static Long getPidByTroubleCode(Connection connection,String troubleCode){
Long pid = null;
/** 查询数据库,获得父键PID**/
String pidSql = "select id from tb_trouble_code t where t.trouble_code = ?";
ResultSet rs = null;
PreparedStatement pStatement = null;
try{
pStatement = connection.prepareStatement(pidSql);
pStatement.setString(1, troubleCode);
rs = pStatement.executeQuery();
while(rs.next()){
pid = rs.getLong("id"); /** 一条记录 **/
}
//System.out.println("-- 获取父键PID成功 -->"+pid);
}catch(SQLException e){
System.out.println("-- 获取父键PID失败 --");
e.printStackTrace();
}
try {
rs.close();
pStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return pid;
}
/** 插入故障码并返回主键PID**/
public static Long getPidByInsertTroubleCode(Connection connection,String troubleCode){
Long pid = null;
/** 为了获得主表的PID,先取sequence,再往主表插入记录。**/
String sequenceSql = "select trouble_code_seq.nextval pid from dual ";
try {
PreparedStatement pr = connection.prepareStatement(sequenceSql);
ResultSet rs = pr.executeQuery();
if(rs.next()){
pid = rs.getLong("pid");
}
System.out.println("-- 获取sequence,返回主键PID成功-->"+pid);
rs.close();
pr.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String troubleCodeSql = "insert into tb_trouble_code values(?,?,'','madp')";
/**
* ID NUMBER(15)
TROUBLE_CODE VARCHAR2(512)
ENTRY_TIME DATE
CREATOR VARCHAR2(60)
*/
try {
PreparedStatement prstmt = connection.prepareStatement(troubleCodeSql, Statement.RETURN_GENERATED_KEYS);
prstmt.setLong(1, pid);
prstmt.setString(2, troubleCode);
prstmt.execute();
// ResultSet rSet = prstmt.getGeneratedKeys();
// while(rSet.next()){
// System.out.println("----"+rSet.getString(1));
// //pid = Integer.valueOf(rSet.getInt(1)).longValue();
// }
System.out.println("-- 插入故障码成功--");
//rSet.close();
prstmt.close();
} catch (SQLException e) {
System.out.println("-- 插入故障码并返回主键PID失败--");
e.printStackTrace();
}
return pid;
}
/** 关闭数据库连接 **/
public static void closeConnection(Connection conn){
try{
if(!conn.isClosed()){
conn.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
/** 插入数据库中没有的车系 表:tb_car_desc *
* @throws ClassNotFoundException
* @throws SQLException */
public static void insertMake(String make) throws ClassNotFoundException, SQLException{
Connection connection = getConnection();
String sql = "select count(1) from tb_car_desc where code = ? and lan_id = 15 ";
try {
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, make);
ResultSet rs = ps.executeQuery();
Long count = null;
while(rs.next()){
count = rs.getLong(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
-- 插入数据成功 --
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at org.apache.commons.httpclient.ContentLengthInputStream.read(ContentLengthInputStream.java:169)
at java.io.FilterInputStream.read(FilterInputStream.java:116)
at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:107)
at java.io.FilterInputStream.read(FilterInputStream.java:90)
at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:126)
at org.apache.commons.httpclient.HttpMethodBase.getResponseBody(HttpMethodBase.java:684)
at test.GrabDTCByHttpClient.getBody(GrabDTCByHttpClient.java:86)
at test.GrabDTCByHttpClient.main(GrabDTCByHttpClient.java:247)
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.<init>(String.java:515)
at test.GrabDTCByHttpClient.main(GrabDTCByHttpClient.java:247)
**/
发表评论
-
Java获取客户端的真实IP地址
2011-01-04 15:16 34028像移动网关一样,iisforward这个ISAPI过滤器也会对 ... -
Nginx做负载均衡时X-Forwarded-For信息头的处理
2011-01-04 15:14 5760如今利用nginx做负载均 ... -
HTTP协议详解---持久连接与非持久连接
2010-11-16 19:45 4472最近写作完HTTP协议一节,略做总结:非持久连接是在http ... -
相关知识
2010-08-11 10:30 12071、网页内嵌pdf,前提是保证该浏览器安装了pdf插件,否则无 ... -
response.setHeader()的用法
2010-08-11 09:52 1745response.setHeader()下载中 ... -
response.setHeader参数、用法的介绍
2010-08-11 09:32 1408response.setHeader 是用来设 ... -
抓取canobd2网站数据
2010-07-29 15:20 1700/* * Created on 2009-10-12 ... -
commons.httpclient.HttpMethodDirector java.net.SocketException: Connection reset
2010-07-29 11:32 2770httpClient使用过程中的问题: http:// ... -
利用htmlparser抓取网页内容
2010-07-07 10:36 1751import org.htmlparser.Node;impo ... -
JAVA实现httpClient实例
2010-07-07 09:52 1283/* * Created on 2009-10-12 ...
相关推荐
白色卡通风格响应式游戏应用商店企业网站模板.zip
智慧工地,作为现代建筑施工管理的创新模式,以“智慧工地云平台”为核心,整合施工现场的“人机料法环”关键要素,实现了业务系统的协同共享,为施工企业提供了标准化、精益化的工程管理方案,同时也为政府监管提供了数据分析及决策支持。这一解决方案依托云网一体化产品及物联网资源,通过集成公司业务优势,面向政府监管部门和建筑施工企业,自主研发并整合加载了多种工地行业应用。这些应用不仅全面连接了施工现场的人员、机械、车辆和物料,实现了数据的智能采集、定位、监测、控制、分析及管理,还打造了物联网终端、网络层、平台层、应用层等全方位的安全能力,确保了整个系统的可靠、可用、可控和保密。 在整体解决方案中,智慧工地提供了政府监管级、建筑企业级和施工现场级三类解决方案。政府监管级解决方案以一体化监管平台为核心,通过GIS地图展示辖区内工程项目、人员、设备信息,实现了施工现场安全状况和参建各方行为的实时监控和事前预防。建筑企业级解决方案则通过综合管理平台,提供项目管理、进度管控、劳务实名制等一站式服务,帮助企业实现工程管理的标准化和精益化。施工现场级解决方案则以可视化平台为基础,集成多个业务应用子系统,借助物联网应用终端,实现了施工信息化、管理智能化、监测自动化和决策可视化。这些解决方案的应用,不仅提高了施工效率和工程质量,还降低了安全风险,为建筑行业的可持续发展提供了有力支持。 值得一提的是,智慧工地的应用系统还围绕着工地“人、机、材、环”四个重要因素,提供了各类信息化应用系统。这些系统通过配置同步用户的组织结构、智能权限,结合各类子系统应用,实现了信息的有效触达、问题的及时跟进和工地的有序管理。此外,智慧工地还结合了虚拟现实(VR)和建筑信息模型(BIM)等先进技术,为施工人员提供了更为直观、生动的培训和管理工具。这些创新技术的应用,不仅提升了施工人员的技能水平和安全意识,还为建筑行业的数字化转型和智能化升级注入了新的活力。总的来说,智慧工地解决方案以其创新性、实用性和高效性,正在逐步改变建筑施工行业的传统管理模式,引领着建筑行业向更加智能化、高效化和可持续化的方向发展。
内容概要:本文提出了一种基于卷积神经网络(CNN)的AV1视频编码环路滤波方法。该方法利用深度可变的简单网络结构SimNet,针对不同量化参数(QP)调整网络深度,从而提高编码效率和视觉质量。同时,作者提出了一种适用于INTER编码的跳过增强策略,以避免重复增强导致的图像质量下降。实验结果表明,该方法在INTRA和INTER编码模式下分别实现了平均7.27%和5.57%的BD-rate降低,且在编码时间上优于AV1基准。 适合人群:视频编码研究人员、AI开发者、多媒体技术专家。 使用场景及目标:适用于提升视频压缩编码的效率和视觉质量,特别是对于AV1视频编码标准的应用。 其他说明:该方法不仅提高了编码效率和视觉质量,还降低了计算复杂度。
白色简洁风格的商业投资组合网站HTML5模板.zip
在线式缠绕膜机自动覆膜缠绕机sw16全套技术资料100%好用.zip
基于ESO的永磁同步电机无感FOC 1.采用线性扩张状态观测器(LESO)估计电机反电势,利用锁相环从反电势中提取位置和转速信息,从而实现无位置传感器控制; 2.提供算法对应的参考文献和仿真模型。 购买赠送PMSM控制相关电子资料。 仿真模型纯手工搭建,不是从网络上复制得到。 仿真模型仅供学习参考。内容来源于网络分享,如有侵权请联系我删除。另外如果没有积分的同学需要下载,请私信我。
白色简洁风格的酒店展示及预订企业网站源码下载.zip
白色简洁风格的享受旅行导航指南整站网站源码下载.zip
白色简洁风格的照片浏览切换源码下载.rar
白色简洁风格的女装商城整站网站源码下载.zip
EnvironmentError.md
白色简洁风格的举重锻炼健身企业网站模板.zip
预览地址:https://blog.csdn.net/qq_42431718/article/details/144758683 html+css+js网页设计 美食 家美食1个页面
白色简洁风格的室内家具展示企业网站源码下载.zip
白色简洁风格的婚礼活动展示信息源码下载.zip
内容概要:本文详细介绍了使用Python绘制圣诞树的方法,包括简单的文本圣诞树、使用Turtle库绘制复杂图形圣诞树,以及添加装饰和动态效果的高级应用。文章通过代码示例和详细的解释,逐步展示了如何实现不同风格的圣诞树。 适合人群:Python初学者,特别是对图形编程感兴趣的读者。 使用场景及目标:① 学习如何使用Python的字符串操作和循环结构绘制文本圣诞树;② 掌握Turtle库的基本用法,绘制复杂的图形圣诞树;③ 添加装饰和动态效果,提升节日氛围。 阅读建议:建议读者跟随代码示例进行实践,结合注释和解释,更好地理解和掌握每个步骤的实现方法。
白色简洁风格的宴席精致餐饮整站网站源码下载.zip
采用STM32F103系列作为主控,利用MLX90614采集温度,配以OLED显示以及flash存储数据
html渲染器,粘贴html代码到这个渲染器即可渲染出对应的效果
随着学业负担的日益加重,越来越多的学生选择通过家教、自学或参加补习班来加强课外学习。然而,家教费用高昂,自学效率低下且难以及时解决疑难问题,而补习班则受限于时间和地点,灵活性不足。此外,国家政策也不鼓励校外补习。鉴于网络技术的成熟和各类在线平台的兴起,开发一个专业的在线辅助学习网站对于辅助学生的课外学习显得尤为重要。 本在线教育系统基于Vue.js构建,采用B/S架构设计,后端语言为Java,数据库使用MySQL。通过整合Vue.js技术,系统界面更加丰富和友好。系统主要面向课程购买用户,涉及的角色包括管理员、学生和教师。学生可以注册登录后浏览课程视频、收藏课程、留言并购买课程,同时实现订单管理。管理员负责管理学生信息、课程信息、发布班级和管理章节等。教师则可以管理课程订单、课程内容和章节。该系统允许学生利用碎片时间自主学习,具有很高的灵活性,对于难以理解的课程可以反复学习并在线提问,极大地促进了学生的学习。