关闭

jdbc basic operation,how to insert a long raw data

标签: insertjdbcbasicstringimportexception
746人阅读 评论(0) 收藏 举报
分类:

红色标志处非常重要!!!

package packCGDataServer;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.*;
import ShapeFile.*;
import DbfFile.*;
import ShapeFile.ShapePoint;

public class DBoper {
 public Connection m_conn;

 public String m_userName;

 public String m_passWord;

 public String m_dataSource;

 public String m_serviceName;

 public String m_portName;

 public boolean initConnection() {
  try {

   DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
   java.util.Properties info = new java.util.Properties();
   info.put("user", m_userName);
   info.put("password", m_passWord);
   // specify the connection object
   m_conn = DriverManager.getConnection("jdbc:oracle:thin:@"
     + m_dataSource + ":" + m_portName + ":" + m_serviceName
     + "", info);
   m_conn.setAutoCommit(false);

   return true;
  } catch (Exception e) {
   System.out.print(e.getMessage());
   return false;
  }
 }

 public boolean executeSQL(String exeStr) {

  try {
   if (m_conn.isClosed() == false) {
    try {
     Statement stmt = m_conn.createStatement();
     stmt.execute(exeStr);
     return true;// jdgStr +" succeeded";
    } catch (Exception e) {
     return false;// jdgStr + " failed";
    }

   } else {
    return false;// jdgStr + " failed";
   }
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return false;

 }

 public boolean addPolygonLayerRecords(ShapefileShape[] ccObJ3,
   String layerID) {
  String tabF = CConfigValue.TABF + layerID;
  try {
   if (m_conn.isClosed() == false) {
    try {
     String content;
     for (int i = 0; i < ccObJ3.length; i++) {
      ShapePolygon ccPolygon = (ShapePolygon) ccObJ3[i];
      content = ccObJ3[i].toString();
      byte[] pts = content.getBytes();
      InputStreamReader bais = new InputStreamReader(
        new ByteArrayInputStream(pts));
      PreparedStatement pstmt = m_conn
        .prepareStatement("insert into "
          + tabF
          + " (FID,FIDTYPE,NUMOFCOUNTS,"
          + "MINX,MINY,MAXX,MAXY,AREA,LEN,POINTS,"
          + "CSRID,MSRID,BDELETED,BRELATEDID,BLOCK,PROCID) values (?,?,?,?,?,?,?,?,?,utl_raw.cast_to_raw(?),?,?,?,?,?,?)");
      long v_FID = i + 1;
      long v_FIDTYPE = 1;
      int v_NUMOFCOUNTS = ccPolygon.getNumPoints();
      double v_MINX = ccPolygon.getBounds()[0];
      double v_MINY = ccPolygon.getBounds()[1];
      double v_MAXX = ccPolygon.getBounds()[2];
      double v_MAXY = ccPolygon.getBounds()[3];
      double v_AREA = 0;
      double v_LEN = 0;
      // POINTS
      String v_CSRID = "1";
      String v_MSRID = "1";
      String v_BDELETED = "0";
      String v_BRELATEDID = "0";
      String v_BLOCK = "0";
      String v_PROCID = "0";

      pstmt.setString(1, String.valueOf(v_FID));
      pstmt.setString(2, String.valueOf(v_FIDTYPE));
      pstmt.setString(3, String.valueOf(v_NUMOFCOUNTS));
      pstmt.setString(4, String.valueOf(v_MINX));
      pstmt.setString(5, String.valueOf(v_MINY));
      pstmt.setString(6, String.valueOf(v_MAXX));
      pstmt.setString(7, String.valueOf(v_MAXY));
      pstmt.setString(8, String.valueOf(v_AREA));
      pstmt.setString(9, String.valueOf(v_LEN));
      pstmt.setCharacterStream(10, bais, pts.length);
      pstmt.setString(11, v_CSRID);
      pstmt.setString(12, v_MSRID);
      pstmt.setString(13, v_BDELETED);
      pstmt.setString(14, v_BRELATEDID);
      pstmt.setString(15, v_BLOCK);
      pstmt.setString(16, v_PROCID);

      // pstmt.setString(1, "kitt");
      // pstmt.setCharacterStream(2, bais, pts.length);
      pstmt.execute();
      pstmt.close();
     }
     m_conn.commit();
     return true;// jdgStr +" succeeded";
    } catch (Exception e) {
     return false;// jdgStr + " failed";
    }

   } else {
    return false;// jdgStr + " failed";
   }
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  return false;
 }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:48642次
    • 积分:719
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:26篇
    • 译文:0篇
    • 评论:1条
    文章分类