vendredi 23 janvier 2015

Connecting Java to SQLite server, basic CRUD functionality.

I am trying to connect my java project to an SQLite database. My end goal is to have methods that will let me Create, Read, Update or Delete records from/to this database.


I have been reading a lot of guides online, but despite a much stronger conceptual understanding of how DAO and SQLite works, I havent been able to build working code so I was hoping someone could help.



package com.rebels.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/* @author - Kieran O' Mahony. Resources consulted include platform.netbeans.org, stackoverflow, and tutorialspoint.
@resources consulted - Danielniko.wordpress.com
*/
public class DBConnect {

@SuppressWarnings("empty-statement")

public static void main(String args) throws ClassNotFoundException, SQLException {

Class.forName("org.sqlite.JDBC");

Connection connection = DriverManager.getConnection("jdbc:sqlite:H:/Third Year/Java/RebelDB.s3db");

try {
Properties prop = new Properties();
InputStream inputStream = DbUtil.class.getClassLoader().getResourceAsStream("/db.properties");
prop.load(inputStream);
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String user = prop.getProperty("user");
String password = prop.getProperty("password");
Class.forName(driver);
connection = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}


Thanks for your time. I will clarify any questions your might have and greatly appreciate any and all help


Aucun commentaire:

Enregistrer un commentaire