Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- /**
- *
- * @author Cusy
- */
- public class JavaApplication1 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws Exception{
- java.sql.Connection con = null;
- Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
- try {
- con = DriverManager.getConnection("jdbc:derby//localhost:1527/sample");
- } catch (java.sql.SQLException sqle) {
- con = DriverManager.getConnection("jdbc:derby//localhost:1527/sample;create=true");
- con.createStatement().executeUpdate("CREATE TABLE PROVA (nome VARCHAR(250), id INT NOT NULL, cognome VARCHAR(250))");
- }
- PreparedStatement s = con.prepareStatement("INSERT INTO PROVA (nome,id,cognome) VALUES(?,?,?)");
- s.setString(1, "Alessandro");
- s.setInt(2, 0);
- s.setString(3, "Roncato");
- s.executeUpdate();
- ResultSet rs = con.createStatement().executeQuery("select * from PROVA");
- while (rs.next()) {
- System.out.println("nome: " + rs.getString(1) + " id:" + rs.getInt(2));
- }
- try {//è una versione embedded quindi va chiusa quando l'applicazione termina
- DriverManager.getConnection("jdbc:derby:sample;shutdown=true");
- } catch (SQLException se) {//Stato = 08006 chiusura OK, altrimenti si è verificato un errore
- if (!se.getSQLState().equals("08006")) {
- throw se;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement