Created
November 25, 2009 13:19
-
-
Save tedheich/242696 to your computer and use it in GitHub Desktop.
Boiler plate code in java database programming - using sqlite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.DriverManager; | |
import java.sql.Connection; | |
import java.sql.Statement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
class DBSample { | |
public static void main(String []args){ | |
Connection cn = null; | |
Statement st = null; | |
ResultSet rs = null; | |
try { | |
Class.forName("org.sqlite.JDBC"); | |
cn = DriverManager.getConnection("jdbc:sqlite:sample.db"); | |
st = cn.createStatement(); | |
rs = st.executeQuery("SELECT * FROM user;"); | |
while(rs.next()) { | |
System.out.print(rs.getString(1)); | |
System.out.print(rs.getString(2)); | |
System.out.println(rs.getString(3)); | |
} | |
} | |
catch(ClassNotFoundException ce) { | |
ce.printStackTrace(); | |
} | |
catch(SQLException sqle) { | |
sqle.printStackTrace(); | |
} | |
finally { | |
try { | |
rs.close(); | |
st.close(); | |
cn.close(); | |
} | |
catch(SQLException sqle) { | |
sqle.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment