Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. import java.sql.*;
  2. /**
  3.  *
  4.  * @author Cusy
  5.  */
  6. public class JavaApplication1 {
  7.  
  8.     /**
  9.      * @param args the command line arguments
  10.      */
  11.     public static void main(String[] args) throws Exception{
  12.         java.sql.Connection con = null;
  13.         Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
  14.        
  15.         try {
  16.             con = DriverManager.getConnection("jdbc:derby//localhost:1527/sample");
  17.         } catch (java.sql.SQLException sqle) {
  18.             con = DriverManager.getConnection("jdbc:derby//localhost:1527/sample;create=true");
  19.             con.createStatement().executeUpdate("CREATE TABLE PROVA (nome VARCHAR(250), id INT NOT NULL, cognome VARCHAR(250))");
  20.         }
  21.  
  22.         PreparedStatement s = con.prepareStatement("INSERT INTO PROVA (nome,id,cognome) VALUES(?,?,?)");
  23.        
  24.         s.setString(1, "Alessandro");
  25.         s.setInt(2, 0);
  26.         s.setString(3, "Roncato");
  27.            
  28.         s.executeUpdate();
  29.  
  30.         ResultSet rs = con.createStatement().executeQuery("select * from PROVA");
  31.         while (rs.next()) {
  32.             System.out.println("nome: " + rs.getString(1) + " id:" + rs.getInt(2));
  33.         }
  34.         try {//è una versione embedded quindi va chiusa quando l'applicazione termina
  35.             DriverManager.getConnection("jdbc:derby:sample;shutdown=true");
  36.         } catch (SQLException se) {//Stato = 08006 chiusura OK, altrimenti si è verificato un errore
  37.             if (!se.getSQLState().equals("08006")) {
  38.                 throw se;
  39.             }
  40.         }
  41.  
  42.     }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement