한마디로 정리하자면, 서버가 아닌 로컬에 저장할 수 있는 가벼운 DB이다!
sqlite+java 예시 코드
코드를 분석하며 SQLite를 공부하였다.
위 코드는 JDBC를 활용하였다.
public static void main( String args[] )
{
connectDB();
createDB();
insertDB();
selectDB();
//updateDB();
//deleteDB();
}
main 메소드는
Database를 생성하고, 연결하는 메소드
내용을 저장하고, 읽어오는 메소드
로 이루어져 있다.
코드를 한줄씩 분석해보자!!
public static void createDB()
{
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:myBlog.sqlite");
System.out.println("Opened database successfully");
stmt = c.createStatement();
String sql = "CREATE TABLE web_blog " +
"(ID INTEGER PRIMARY KEY autoincrement," +
" NAME CHAR(50) NOT NULL, " +
" message TEXT NOT NULL, " +
" date_added datetime)";
stmt.executeUpdate(sql);
stmt.close();
c.close();
catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Table created successfully");
}
public static void connectDB()
{
Connection c = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:myBlog.sqlite");
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Opened database successfully");
}
public static void insertDB()
{
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:myBlog.sqlite");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
String sql = "INSERT INTO web_blog (NAME,message,date_added) " +
"VALUES ('Ken', 'Hello every one!!!', datetime())," +
" ('Jim', 'whats up!!!',datetime());";
stmt.executeUpdate(sql);
stmt.close();
c.commit();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Records created successfully");
}
public static void selectDB()
{
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:myBlog.sqlite");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT * FROM web_blog;" );
while ( rs.next() ) {
int id = rs.getInt("id");
String name = rs.getString("name");
String message = rs.getString("message");
String date_added = rs.getString("date_added");
System.out.println( "ID : " + id );
System.out.println( "Name : " + name );
System.out.println( "Message : " + message );
System.out.println( "Date Added : " + date_added );
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
public static void updateDB()
{
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:myBlog.sqlite");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
String sql = "UPDATE web_blog set message = 'This is updated by updateDB()' where ID=1;";
stmt.executeUpdate(sql);
c.commit();
ResultSet rs = stmt.executeQuery( "SELECT * FROM web_blog;" );
while ( rs.next() ) {
int id = rs.getInt("id");
String name = rs.getString("name");
String message = rs.getString("message");
String date_added = rs.getString("date_added");
System.out.println( "ID : " + id );
System.out.println( "Name : " + name );
System.out.println( "Message : " + message );
System.out.println( "Date Added : " + date_added );
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
public static void deleteDB()
{
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:myBlog.sqlite");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
String sql = "DELETE from web_blog where ID=1;";
stmt.executeUpdate(sql);
c.commit();
ResultSet rs = stmt.executeQuery( "SELECT * FROM web_blog;" );
while ( rs.next() ) {
int id = rs.getInt("id");
String name = rs.getString("name");
String message = rs.getString("message");
String date_added = rs.getString("date_added");
System.out.println( "ID : " + id );
System.out.println( "Name : " + name );
System.out.println( "Message : " + message );
System.out.println( "Date Added : " + date_added );
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
}