KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > toursdb > insertMaps


1 /*
2
3    Derby - Class SimpleApp
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package toursdb;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.Connection JavaDoc;
29 import java.sql.DriverManager JavaDoc;
30 import java.math.BigDecimal JavaDoc;
31 import java.sql.Clob JavaDoc;
32 import java.sql.SQLException JavaDoc;
33
34
35 public class insertMaps {
36
37     public static final String JavaDoc CSdriver = new String JavaDoc("org.apache.derby.jdbc.EmbeddedDriver");
38     public static final String JavaDoc dbURLCS = new String JavaDoc("jdbc:derby:toursDB");
39
40     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
41
42         try {
43             Connection JavaDoc connCS = null;
44
45             System.out.println("Loading the Cloudscape jdbc driver...");
46             Class.forName(CSdriver).newInstance();
47     
48             System.out.println("Getting Cloudscape database connection...");
49             connCS = DriverManager.getConnection(dbURLCS);
50             System.out.println("Successfully got the Cloudscape database connection...");
51
52             PreparedStatement JavaDoc ps = null;
53
54             ps = connCS.prepareStatement
55             ("insert into maps (map_name, region, area, photo_format, picture) values (?,?,?,?,?)");
56     
57             ps.setString(1,"BART");
58             ps.setString(2,"Bay Area");
59             ps.setBigDecimal(3, new BigDecimal JavaDoc("1776.11"));
60             ps.setString(4,"gif");
61             File JavaDoc file = new File JavaDoc ("BART.gif");
62             InputStream JavaDoc fileIn = new FileInputStream JavaDoc(file);
63             ps.setBinaryStream(5, fileIn, (int)file.length());
64             int numrows = ps.executeUpdate();
65
66             ps.setString(1,"Caltrain");
67             ps.setString(2,"West Bay");
68             ps.setBigDecimal(3, new BigDecimal JavaDoc("1166.77"));
69             ps.setString(4,"gif");
70             file = new File JavaDoc ("Caltrain.gif");
71             fileIn = new FileInputStream JavaDoc(file);
72             ps.setBinaryStream(5, fileIn, (int)file.length());
73             numrows = numrows + ps.executeUpdate();
74
75             ps.setString(1,"Light Rail");
76             ps.setString(2,"Santa Clara Valley");
77             ps.setBigDecimal(3, new BigDecimal JavaDoc("9117.90"));
78             ps.setString(4,"gif");
79             file = new File JavaDoc ("BART.gif");
80             fileIn = new FileInputStream JavaDoc(file);
81             ps.setBinaryStream(5, fileIn, (int)file.length());
82             numrows = numrows + ps.executeUpdate();
83
84             System.out.println("Inserted " + numrows + " rows into the ToursDB");
85
86             ps.close();
87     
88             connCS.close();
89
90         } catch (SQLException JavaDoc e) {
91             System.out.println ("FAIL -- unexpected exception: " + e.toString());
92             e.printStackTrace();
93         } catch (Exception JavaDoc e) {
94             System.out.println ("FAIL -- unexpected exception: " + e.toString());
95             e.printStackTrace();
96         }
97
98     }
99
100 }
101
Popular Tags