home *** CD-ROM | disk | FTP | other *** search
Java Source | 2003-05-20 | 6.8 KB | 270 lines |
- package nl.pcactive.db;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.Driver;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Properties;
-
- import nl.pcactive.bean.FamilyBean;
- import nl.pcactive.bean.IndividualBean;
- import nl.pcactive.bean.SessionBean;
-
- /**
- * <p>Description: GEDCOM 2 DATABASE</p>
- * <p>Copyright: Copyright (c) 2002</p>
- * <p>Company: Pc-Active</p>
- * @author Benny Lootens
- * @version 2.0
- */
- public class Jdbc {
-
- private static Driver driver;
- private static Properties props = new Properties();
- private static String url, drv, user, pwd, gedcom, type;
-
- /** if you use other tables name, you can change them here */
- public interface Tables {
-
- String INDIVIDUAL = "t_individual";
- String FAMILY = "t_family";
- String CHILD = "t_child";
-
- String SESSION = "t_session";
- }
-
- static {
- File f = null;
-
- try {
-
- System.err.println("Connecting to database...");
-
- f = new File("ged.ini");
-
- props.load(new FileInputStream(f));
-
- drv = props.getProperty("db.driver");
- url = props.getProperty("db.url");
- user = props.getProperty("user");
- pwd = props.getProperty("password");
- gedcom = props.getProperty("gedcom");
- type = props.getProperty("db.type");
-
- driver = (Driver) Class.forName(drv).newInstance();
-
- // factor = Integer.parseInt(properties.getProperty("factor"));
- // if (factor == 0) factor = 10;
-
- System.err.println("props=" + props.toString());
- } catch (FileNotFoundException ff) {
- try {
- System.err.println(
- "problems with ini-file " + f.getCanonicalPath());
- } catch (IOException e) {
- }
- } catch (Throwable t) {
- t.printStackTrace(System.err);
- }
- }
-
- /** @return a database connection */
- public static Connection openConnection() throws SQLException {
- return driver.connect(url, props);
- }
-
- /** cleanup code for database objects */
- public static void close(Object o) {
-
- if (o == null)
- return;
-
- try {
-
- if (o instanceof ResultSet)
- ((ResultSet) o).close();
-
- else if (o instanceof Statement)
- ((Statement) o).close();
-
- else if (o instanceof Connection)
- ((Connection) o).close();
-
- } catch (Exception ignore) {
- } finally {
- o = null;
- }
- }
-
- /** create the database tables necessary to run ged2db */
- public static void createDatabase() {
-
- Statement stmt = null;
- Connection con = null;
-
- try {
- con = Jdbc.openConnection();
- stmt = con.createStatement();
-
- if ("mysql".equals(type)) {
- stmt.executeUpdate("drop table if exists " + Tables.INDIVIDUAL);
- stmt.executeUpdate("drop table if exists " + Tables.FAMILY);
- stmt.executeUpdate("drop table if exists " + Tables.CHILD);
- }
-
- stmt.executeUpdate(
- " create table "
- + Tables.INDIVIDUAL
- + "("
- + " Individual int not null,"
- + " FirstName varchar(255) not null,"
- + " MiddleName varchar(255) not null,"
- + " LastName varchar(255) not null,"
- + " Sex varchar(6) null,"
- + " BirthDate varchar(255) null,"
- + " BirthPlace varchar(255) null,"
- + " BaptismDate varchar(255) null,"
- + " BaptismPlace varchar(255) null,"
- + " DeathDate varchar(255) null,"
- + " DeathPlace varchar(255) null,"
- + " BurriedDate varchar(255) null,"
- + " BurriedPlace varchar(255) null,"
- + " Notes "
- + ("mysql".equals(type) ? "blob" : "varchar(1000)")
- + " null)");
- stmt.executeUpdate(
- "create table "
- + Tables.FAMILY
- + "("
- + " Family int not null,"
- + " Husband int not null,"
- + " Wife int not null,"
- + " MarriedDate varchar(255) null,"
- + " MarriedPlace varchar(255) null)");
- stmt.executeUpdate(
- "create table "
- + Tables.CHILD
- + "("
- + " Family int not null,"
- + " Child int not null)");
- } catch (Exception e) {
- e.printStackTrace(System.err);
- } finally {
- Jdbc.close(stmt);
- Jdbc.close(con);
- }
- }
-
- /** add an individual to the database */
- public static void addIndividual(IndividualBean individual, Statement stmt)
- throws SQLException {
- stmt.executeUpdate(
- "insert into "
- + Tables.INDIVIDUAL
- + " values("
- + individual.getId()
- + ", '"
- + individual.getFirst()
- + "', '"
- + individual.getMiddle()
- + "', '"
- + individual.getLast()
- + "', '"
- + individual.getSex()
- + "', '"
- + individual.getBirth().getDate()
- + "', '"
- + individual.getBirth().getPlace()
- + "', '"
- + individual.getBaptism().getDate()
- + "', '"
- + individual.getBaptism().getPlace()
- + "', '"
- + individual.getDeath().getDate()
- + "', '"
- + individual.getDeath().getPlace()
- + "', '"
- + individual.getBurried().getDate()
- + "', '"
- + individual.getBurried().getPlace()
- + "', '"
- + individual.getNotes()
- + "')");
- }
-
- /** functie voor het toevoegen van een familie in t_family */
- public static void addFamily(FamilyBean family1, Statement stmt)
- throws SQLException {
- stmt.executeUpdate(
- "insert into "
- + Tables.FAMILY
- + " values("
- + family1.getId()
- + ", "
- + family1.getHusband()
- + ", "
- + family1.getWife()
- + ", '"
- + family1.getMarried().getDate()
- + "', '"
- + family1.getMarried().getPlace()
- + "')");
- }
-
- /** add a child to the database */
- public static void addChild(String s, String s1, Statement stmt)
- throws SQLException {
- stmt.executeUpdate(
- "insert into " + Tables.CHILD + " values(" + s + ", " + s1 + ")");
- }
-
- /** @return the name of the input .gedcom file */
- public static String getGedcom() {
- return gedcom;
- }
- public String getType() {
- return type;
- }
-
- /** save the session in the database */
- public static void store(SessionBean bean) {
- Connection con = null;
- PreparedStatement pstmt = null;
-
- try {
- con = Jdbc.openConnection();
- pstmt =
- con.prepareStatement(
- "insert into " + Tables.SESSION
- + "(sessionid,remotehost,remoteip,timestamp,"
- + "referer,useragent,xforwardedfor)"
- + " values(?,?,?,?,?,?,?)");
- pstmt.setString(1, bean.getSessionId());
- pstmt.setString(2, bean.getRemoteHost());
- pstmt.setString(3, bean.getRemoteIp());
- pstmt.setString(4, bean.getTimeStamp());
- pstmt.setString(5, bean.getReferer());
- pstmt.setString(6, bean.getUser_agent());
- pstmt.setString(7, bean.getX_forwarded_for());
- pstmt.executeUpdate();
-
- System.out.println(
- "session stored: "
- + bean.getTimeStamp()
- + "/"
- + bean.getSessionId());
- } catch (Exception e) {
- e.printStackTrace(System.err);
- } finally {
- Jdbc.close(pstmt);
- Jdbc.close(con);
- }
- }
-
- }