利用oracle提供的一個工具類可以很方便地把shape文件寫入oracle數據庫
主要是 oracle.spatial.util.SampleShapefileToJGeomFeature 類
該類提供了main()方法,直接傳遞數據庫相關信息即可:
下面是一個我寫的源碼示例:
1 import java.io.File;
2 import java.io.FileNotFoundException;
3 import java.sql.Connection;
4 import java.sql.Statement;
5
6 import javax.swing.JFileChooser;
7 import javax.swing.filechooser.FileFilter;
8
9 import oracle.spatial.util.SampleShapefileToJGeomFeature;
10
11 import demo.Connect;
12
13
14 public class UploadShapeFileToOracleDB {
15 public static void main(String[] args) throws Exception {
16 updata(args);
17 }
18
19 /**
20 * Prompt for File if not provided on the command line. Don't forget the
21 * quotes around your path if there are spaces!
22 *
23 * @throws FileNotFoundException
24 * 打開shp文件對話框 獲取相關參數 用到了兩個重要的
25 * 系統類 (1)FIle類 (2)JFileChooser類
26 */
27 private static File promptShapeFile(String[] args)
28 throws FileNotFoundException {
29 File file;
30 /*
31 *
32 * 如果沒有輸入參數執行下面的代碼
33 */
34 if (args.length == 0) {
35 JFileChooser chooser = new JFileChooser();// 文件對話框
36 chooser.setDialogTitle("Open Shapefile for Reprojection");
37 /*
38 * FileFilter為抽象類 需要實例化兩個方法 (1)accept(); (2)getDescription();
39 */
40 chooser.setFileFilter(new FileFilter() {
41 public boolean accept(File f) {
42 return f.isDirectory() || f.getPath().endsWith("shp")
43 || f.getPath().endsWith("SHP");
44 }
45 public String getDescription() {
46 return "Shapefiles";
47 }
48 });
49 /*
50 * 打開對話框
51 */
52 int returnVal = chooser.showOpenDialog(null);
53
54 /*
55 * 判斷是夠選擇了yes還是NO
56 */
57 if (returnVal != JFileChooser.APPROVE_OPTION) {
58 System.exit(0);
59 }
60
61 file = chooser.getSelectedFile();
62 /*
63 * 成功輸出正確的文件名
64 */
65 System.out
66 .println("You chose to open this file: " + file.getName());
67 } else {
68 /*
69 * 直接提供參數說明 *
70 */
71 file = new File(args[0]);
72 }
73 /*
74 * 最后驗證file是否存在 如果不存在則顯示拋出異常
75 */
76 if (!file.exists()) {
77 throw new FileNotFoundException(file.getAbsolutePath());
78 }
79 return file;
80 }
81 /**
82 *
83 * @param args
84 * @throws Exception
85 */
86 public static void updata(String[] args) throws Exception {
87 /*
88 * -h 127.0.0.1 host: 127.0.0.1
89 * -p 1521 port: 1521
90 -s orcl111 sid: orcl111
91 -u spatial db_username: spatial
92 -d spatial db_password: spatial
93 -t us_cities db_tablename: us_cities
94 -f shp_cities shapefile_name: shp_cities
95 -r 8307 SRID: 8307
96 */
97 String tableName="tabletest";
98 String[] param=new String[16];
99
100 param[0]="-h";
101 param[1]="127.0.0.1";
102
103 param[2]="-p";
104 param[3]="1521";
105
106 param[4]="-s";
107 param[5]="XE ";
108
109 param[6]="-u";
110 param[7]="system";
111
112 param[8]="-d";
113 param[9]="root";
114
115 param[10]="-t";
116 param[11]=tableName;
117
118 param[12]="-f";
119
120 param[14]="-r";
121 param[15]="8307";
122
123 File file = promptShapeFile(args);
124 String shapeFileName = file.getPath();
125 shapeFileName=shapeFileName.replaceAll(".shp", "");
126
127 param[13]=shapeFileName;
128
129 /*
130 * 把數據插入oracle數據庫
131 * */
132 SampleShapefileToJGeomFeature.main(param);
133
134 /*
135 * 給新建的表建立空間索引
136 */
137 Connection connection=Connect.getConnect();
138 Statement stmt = connection.createStatement();
139 String INDEX="CREATE Index "+tableName+"_idx ON "+ tableName +"(GEOMETRY) INDEXTYPE is MDSYS.SPATIAL_INDEX";
140 stmt.execute(INDEX);
141 stmt.close();
142 connection.close();
143 }
144 }
Connect類為 :
1 package demo; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 7 public class Connect { 8 private static String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE"; 9 private static String username = "system"; 10 private static String pw = "root"; 11 private static Connection conn = null; 12 13 public static Connection getConnect() { 14 try { 15 Class.forName("oracle.jdbc.driver.OracleDriver"); 16 try { 17 conn = DriverManager.getConnection(url, username, pw); 18 } catch (SQLException e) { 19 e.printStackTrace(); 20 } 21 } catch (ClassNotFoundException e) { 22 e.printStackTrace(); 23 } 24 return conn; 25 } 26 27 public void testConnect() { 28 Connection con = getConnect(); 29 if (con == null) 30 System.out.print("連接失敗"); 31 else 32 System.out.print("連接成功"); 33 } 34 }
利用geotools也可實現shape文件存入oralce數據庫,但直接利用oracle提供的類來實現shape文件存入oracle數據庫還是很方便的!
