java原生调用数据库

package com.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.lang.reflect.Field;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import util.CommonUtils;

import com.entity.tdsfsave.JbxxqForm;
import com.entity.tdsfsave.NsrjmxxGridlb;
import com.entity.tdsfsave.TdsfSaveMain;
import com.entity.tdsfsave.TdsfmxGridlb;
import com.entity.tdsfsave.UserListlb;

public class SendTdsXX {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int cnt=0;
  //数据库连接
  System.out.print("START...");//报文头
  String head="|gt3serviceAnd|SWZJ.HXZG.YSH.BCXXCJBSJ|<taxML xsi:type=\"HXZGYSH00006Request\" bbh=\"\" xmlbh=\"\" xmlmc=\"\" xsi:schemaLocation=\"http://www.chinatax.gov.cn/dataspec/ TaxMLBw_HXZG_YSH_00006_Request_V1.1.xsd\" xmlns=\"http://www.chinatax.gov.cn/dataspec/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n" + 
  "<fileInfor>\r\n" + 
  "  <fileGrid>\r\n" + 
  "   <fileGridlb>\r\n" + 
  "    <filepath>2</filepath>\r\n" + 
  "    <filetype>2</filetype>\r\n" + 
  "    <filename>2</filename>\r\n" + 
  "    <fileindex>2</fileindex>\r\n" + 
  "   </fileGridlb>\r\n" + 
  "  </fileGrid>\r\n" + 
  "  <fileInforVO>\r\n" + 
  "   <fileSize>3</fileSize>\r\n" + 
  "   <recordNumber>3</recordNumber>\r\n" + 
  "   <nsrSwjgDm>3</nsrSwjgDm>\r\n" + 
  "  </fileInforVO>\r\n" + 
  " </fileInfor>\r\n";
        String tail="|JSGS.NFWB.DZSWJ|JSGS.NFWB.DZSWJ";
  String driverName = "oracle.jdbc.driver.OracleDriver";
  String url = "jdbc:oracle:thin:@77.24.16.245:1521/gt3";
  String user="gt3";
  String password = "gt3oracle";
  ResultSet rs = null;
  Connection conn = null;
  Statement pstmt =null;
  Class cls=null;
  TdsfSaveMain tdsfSaveMain=null;
  try{  
   //每一行去构造报文 ,生成TdsfSaveMain
   while(true)
   {
    Class.forName(driverName);
    conn = DriverManager.getConnection(url,user,password);
    pstmt = conn.createStatement();
    CallableStatement cs = conn.prepareCall("{call PROC_TDSF_GETMX(?)}");
    cs.setObject(1,"13205110000");
    cs.execute();
    cs.close();
    String sql =" SELECT * FROM tmp_main";
    rs = pstmt.executeQuery(sql);
    ResultSetMetaData md=rs.getMetaData();
    int columnCount = md.getColumnCount();
    while(rs.next())
    {
     //给TdsfSaveMain对象赋值
     tdsfSaveMain= new TdsfSaveMain();
     cls=tdsfSaveMain.getClass();
     for(int idx=1;idx<=columnCount;idx++){
      String colName=md.getColumnName(idx);
      String val=rs.getString(idx);
      if(val==null)
      {
       val="";
      }
      if(md.getColumnTypeName(idx).equals("DATE") ){
       val=val.split(" ")[0];
      }
      Field[] fields=cls.getDeclaredFields();
      for(int i=0;i<fields.length;i++){
       Field f=fields[i];
       f.setAccessible(true);
       try{
        String className=f.getName();
        if(className.toUpperCase().equals(colName.toUpperCase())){
         f.set(tdsfSaveMain, val);
         break;
        }
       }catch(Exception e){
        e.printStackTrace();
       }
      }
      
     }
    }
    
    sql =" SELECT * FROM tmp_BW_GT3_TDSF_SQ_JBXXQFORM";
    rs = pstmt.executeQuery(sql);
    md=rs.getMetaData();
    columnCount = md.getColumnCount();
    while(rs.next())
    {
     //给TdsfSaveMain对象赋值
     JbxxqForm jbxxqForm = new JbxxqForm();
     cls=jbxxqForm.getClass();
     for(int idx=1;idx<=columnCount;idx++){
      String colName=md.getColumnName(idx);
      String val=rs.getString(idx);
      if(val==null)
      {
       val="";
      }
      if(md.getColumnTypeName(idx).equals("DATE") ){
       val=val.split(" ")[0];
      }
      Field[] fields=cls.getDeclaredFields();
      for(int i=0;i<fields.length;i++){
       Field f=fields[i];
       f.setAccessible(true);
       try{
        String className=f.getName();
        if(className.toUpperCase().equals(colName.toUpperCase())){
         f.set(jbxxqForm, val);
         break;
        }
       }catch(Exception e){
        e.printStackTrace();
       }
      }
      
     }
     tdsfSaveMain.setJbxxqForm(jbxxqForm);
    }
    //封装tdsfmxGridlb
    sql =" SELECT * FROM tmp_BW_GT3_TDSF_SQ_TDSFMX";
    rs = pstmt.executeQuery(sql);
    md=rs.getMetaData();
    columnCount = md.getColumnCount();
    while(rs.next())
    {
     //给TdsfSaveMain对象赋值
     TdsfmxGridlb tdsfmxGridlb = new TdsfmxGridlb();
     cls=tdsfmxGridlb.getClass();
     for(int idx=1;idx<=columnCount;idx++){
      String colName=md.getColumnName(idx);
      String val=rs.getString(idx);
      if(val==null)
      {
       val="";
      }
      if(md.getColumnTypeName(idx).equals("DATE") ){
       val=val.split(" ")[0];
      }
      Field[] fields=cls.getDeclaredFields();
      for(int i=0;i<fields.length;i++){
       Field f=fields[i];
       f.setAccessible(true);
       try{
        String className=f.getName();
        if(className.toUpperCase().equals(colName.toUpperCase())){
         f.set(tdsfmxGridlb, val);
         break;
        }
       }catch(Exception e){
        e.printStackTrace();
       }
      }
      
     }
     tdsfSaveMain.addTdsfmxGridlb(tdsfmxGridlb);
    }
    //封装nsrjmxxGridlb
    sql =" SELECT * FROM tmp_BW_GT3_TDSF_SQ_NSRJMXX";
    rs = pstmt.executeQuery(sql);
    md=rs.getMetaData();
    columnCount = md.getColumnCount();
    while(rs.next())
    {
     //给TdsfSaveMain对象赋值
     NsrjmxxGridlb nsrjmxxGridlb = new NsrjmxxGridlb();
     cls=nsrjmxxGridlb.getClass();
     for(int idx=1;idx<=columnCount;idx++){
      String colName=md.getColumnName(idx);
      String val=rs.getString(idx);
      if(val==null)
      {
       val="";
      }
      if(md.getColumnTypeName(idx).equals("DATE") ){
       val=val.split(" ")[0];
      }
      Field[] fields=cls.getDeclaredFields();
      for(int i=0;i<fields.length;i++){
       Field f=fields[i];
       f.setAccessible(true);
       try{
        String className=f.getName();
        if(className.toUpperCase().equals(colName.toUpperCase())){
         f.set(nsrjmxxGridlb, val);
         break;
        }
       }catch(Exception e){
        e.printStackTrace();
       }
      }
      
     }
     tdsfSaveMain.addNsrjmxxGridlb(nsrjmxxGridlb);
    }
    //封装UserListlb
    sql =" SELECT * FROM tmp_GT3_TDSF_SQ_USERLIST";
    rs = pstmt.executeQuery(sql);
    md=rs.getMetaData();
    columnCount = md.getColumnCount();
    while(rs.next())
    {
     //给TdsfSaveMain对象赋值
     UserListlb userListlb = new UserListlb();
     cls=userListlb.getClass();
     for(int idx=1;idx<=columnCount;idx++){
      String colName=md.getColumnName(idx);
      String val=rs.getString(idx);
      if(val==null)
      {
       val="";
      }
      if(md.getColumnTypeName(idx).equals("DATE") ){
       val=val.split(" ")[0];
      }
      Field[] fields=cls.getDeclaredFields();
      for(int i=0;i<fields.length;i++){
       Field f=fields[i];
       f.setAccessible(true);
       try{
        String className=f.getName();
        if(className.toUpperCase().equals(colName.toUpperCase())){
         f.set(userListlb, val);
         break;
        }
       }catch(Exception e){
        e.printStackTrace();
       }
      }
      
     }
     tdsfSaveMain.addUserListlb(userListlb);
    }
    String fsbw=head+tdsfSaveMain.toString()+tail;
    System.out.println(cnt++);
    String sqid=tdsfSaveMain.getID();
    PreparedStatement stat=conn.prepareStatement("update BW_GT3_TDSF_SQ_MAIN a set a.fsrq=sysdate,A.IS_FS='Y',a.fsbw=? where id=?");
    StringReader reader=new StringReader(fsbw);
    stat.setCharacterStream(1,reader,fsbw.length());
    stat.setString(2, sqid);
    stat.executeUpdate();
    conn.commit();
    stat.close();
    /*
    //设置连接超时,10秒则超时
    Socket socket  = new Socket();
    socket.connect(new InetSocketAddress("77.24.19.158",6066),10000);
    //设置socket读超时时间1分钟
    socket.setSoTimeout(60000);
    
    OutputStream os = socket.getOutputStream();
    PrintWriter pw = new PrintWriter(os);
    InputStream is = socket.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    pw.write(head+a.toString()+tail);
    pw.flush();
    socket.shutdownOutput();
    //保存发送报文
    PreparedStatement stat=conn.prepareStatement("update BW_GT3_TDSF_SQ_MAIN a set a.fsrq=sysdate,A.IS_FS='Y',a.fsbw=? where id=?");
    StringReader reader=new StringReader(fsbw);
    stat.setCharacterStream(1,reader,fsbw.length());
    stat.setString(2, a.getID());
    stat.executeUpdate();
    conn.commit();
    stat.close();
    String info=null;
    
    try{
     while((info = br.readLine())!=null)
     {
      //保存返回结果
      int begin =info.indexOf("<rtn_code>");
      int end =info.indexOf("</rtn_code>");
      String rtn_code=info.substring(begin+10,end);
      begin =info.indexOf("<Reason>");
      end =info.indexOf("</Reason>");
      String rtn_msg=info.substring(begin+8,end);
      System.out.println(info);
      stat=conn.prepareStatement("update BW_GT3_TDSF_SQ_MAIN a set a.rtn_code=?,a.rtn_msg=?,a.fhbw=? where id=?");
      reader=new StringReader(info);
      stat.setString(1,rtn_code);
      stat.setString(2,rtn_msg);
      stat.setCharacterStream(3,reader,info.length());
      stat.setString(4, a.getID());
      stat.executeUpdate();
      conn.commit();
      
     }
    }catch(SocketTimeoutException e){
     e.printStackTrace();
     stat=conn.prepareStatement("update BW_GT3_TDSF_SQ_MAIN a set a.rtn_msg='Socket read timeout',a.rtn_code='9' where id=?");
     stat.setString(1, a.getID());
     stat.executeUpdate();
     conn.commit();
    }
    br.close();
    is.close();
    pw.close();
    os.close();
    socket.close(); */  
   }
   
   
  }catch(Exception e){
   e.printStackTrace();
  }finally
  {
   try {
    if(rs!=null)
     rs.close(); 
    if(conn!=null)
     conn.close();
    if(pstmt!=null)
     pstmt.close();
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
  }
  
  
  
 }

}

;