| 1 21 22 package toursdb; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.InputStream ; 27 import java.sql.PreparedStatement ; 28 import java.sql.Connection ; 29 import java.sql.DriverManager ; 30 import java.math.BigDecimal ; 31 import java.sql.Clob ; 32 import java.sql.SQLException ; 33 34 35 public class insertMaps { 36 37 public static final String CSdriver = new String ("org.apache.derby.jdbc.EmbeddedDriver"); 38 public static final String dbURLCS = new String ("jdbc:derby:toursDB"); 39 40 public static void main(String [] args) throws Exception { 41 42 try { 43 Connection 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 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 ("1776.11")); 60 ps.setString(4,"gif"); 61 File file = new File ("BART.gif"); 62 InputStream fileIn = new FileInputStream (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 ("1166.77")); 69 ps.setString(4,"gif"); 70 file = new File ("Caltrain.gif"); 71 fileIn = new FileInputStream (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 ("9117.90")); 78 ps.setString(4,"gif"); 79 file = new File ("BART.gif"); 80 fileIn = new FileInputStream (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 e) { 91 System.out.println ("FAIL -- unexpected exception: " + e.toString()); 92 e.printStackTrace(); 93 } catch (Exception e) { 94 System.out.println ("FAIL -- unexpected exception: " + e.toString()); 95 e.printStackTrace(); 96 } 97 98 } 99 100 } 101 | Popular Tags |