用程序把shape文件直接写入oracle数据库——java
利用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数据库还是很方便的!
以上是 用程序把shape文件直接写入oracle数据库——java 的全部内容, 来源链接: utcz.com/z/393478.html