package test;import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import org.sqlite.JDBC; public class TestSqlLite { public static void main(String[] args) { Connection cn = null; Statement st = null; PreparedStatement ps = null; ResultSet rs = null; String sql = null; try { // 드라이버 존재유무 파악 및 로드 Class.forName("org.sqlite.JDBC"); cn = DriverManager.getConnection( "jdbc:sqlite:developia.db"); st = cn.createStatement(); // star 테이블의 존재 유무 확인 후 존재하지 않으면 생성 rs = st.executeQuery("select count(*) as cnt from sqlite_master where name='star'"); if (rs.next()){ if(rs.getInt("cnt") == 0) { st.execute( "create table star( " + "id integer primary key, " + "name text,job text)" ); } } sql = "insert into star(name,job) values(?,?)"; ps = cn.prepareStatement(sql); ps.setString(1, "서태지"); ps.setString(2, "뮤지션"); ps.executeUpdate(); sql = "select * from star"; rs = st.executeQuery(sql); System.out.println("번호\t이름\t직업"); System.out.println("------------------------"); while (rs.next()) { System.out.print(rs.getInt("id") + "\t"); System.out.print(rs.getString("name") + "\t"); System.out.print(rs.getString("job") + "\n"); } } catch (Exception e) { System.out.println("SQLite 연결 에로 : " + e); } finally { if (rs != null) try{rs.close();} catch(Exception e){} if (ps != null) try{ps.close();} catch(Exception e){} if (st != null) try{st.close();} catch(Exception e){} if (cn != null) try{cn.close();} catch(Exception e){} } } } 출처: http://cafe.naver.com/2developia/2216
TestSqlLite.java
0.0MB
sqlite-jdbc-3.5.9.jar
1.66MB
sqlitebrowser_200_b1_win.zip
6.93MB
'SQL' 카테고리의 다른 글
[복사되는블로그 꼬마갱이] 이 명령과 연결된 DataReader가 이미 열려 있습니다. 먼저 닫아야 합니다. (System.Data) (0) | 2012.12.04 |
---|---|
MS-SQL 프로시져/펑션 내용보기 (0) | 2012.11.09 |
[펌][MS-SQL] 문자열 함수 (0) | 2011.12.28 |
MS-SQL convert (0) | 2011.11.01 |
MS-SQL LPAD/RPAD - REPLICATE (0) | 2011.11.01 |