Java/Database SQL JDBC/HSQL

Материал из Java эксперт
Версия от 06:33, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Get Parameter MetaData From HSQL JDBC Driver

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection     conn = getHSQLConnection();
    Statement st = conn.createStatement();
//    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
    
    
    PreparedStatement pstmt = null;
    ParameterMetaData paramMetaData = null;
    String query = "select * from survey where id > ? ";
    System.out.println("conn=" + conn);
    pstmt = conn.prepareStatement(query);
    paramMetaData = pstmt.getParameterMetaData();
    if (paramMetaData == null) {
      System.out.println("db vendor does NOT support ParameterMetaData");
    } else {
      System.out.println("db vendor supports ParameterMetaData");
      // find out the number of dynamic parameters
      int paramCount = paramMetaData.getParameterCount();
      System.out.println("paramCount=" + paramCount);
    }
    pstmt.close();
    conn.close();
  }
  private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    System.out.println("Driver Loaded.");
    String url = "jdbc:hsqldb:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
  public static Connection getMySqlConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/demo2s";
    String username = "oost";
    String password = "oost";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
  public static Connection getOracleConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:caspian";
    String username = "mp";
    String password = "mp2";
    Class.forName(driver); // load Oracle driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
}





Setup hsqldb Connection and update

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
public class MainClass {
  public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;
    try {
      Class.forName("org.hsqldb.jdbcDriver").newInstance();
      String url = "jdbc:hsqldb:hsqldb\\demoDatabase";
      connection = DriverManager.getConnection(url, "username", "password");
      connection.setAutoCommit(false);
      statement = connection.createStatement();
      String update1 = "UPDATE employees SET email = "a@b.ru" WHERE email = "a@a.ru"";
      statement.executeUpdate(update1);
      Savepoint savepoint1 = connection.setSavepoint("savepoint1");
      String update2 = "UPDATE employees SET email = "b@b.ru" WHERE email = "b@c.ru"";
      statement.executeUpdate(update2);
      Savepoint savepoint2 = connection.setSavepoint("savepoint2");
      String update3 = "UPDATE employees SET email = "c@c.ru" WHERE email = "c@d.ru"";
      statement.executeUpdate(update3);
      Savepoint savepoint3 = connection.setSavepoint("savepoint3");
      String update4 = "UPDATE employees SET email = "d@d.ru" WHERE email = "d@e.ru"";
      statement.executeUpdate(update4);
      Savepoint savepoint4 = connection.setSavepoint("savepoint4");
      String update5 = "UPDATE employees SET email = "e@e.ru" WHERE email = "e@f.ru"";
      statement.executeUpdate(update5);
      Savepoint savepoint5 = connection.setSavepoint("savepoint5");
      connection.rollback(savepoint3);
      connection.rumit();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (statement != null) {
        try {
          statement.close();
        } catch (SQLException e) {
        } // nothing we can do
      }
      if (connection != null) {
        try {
          connection.close();
        } catch (SQLException e) {
        } // nothing we can do
      }
    }
  }
}