import java.sql.Connection; import java.sql.*; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.ResultSet; import java.sql.Statement; public class JdbcConnectionExample { public static void main(String[] args) throws ClassNotFoundException { Connection con = null; Statement st = null; String url = "jdbc:oracle:thin:@//localhost:1521/XE"; String username = "system"; String password = "1234"; try { Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection(url, username, password); System.out.println("Connected!"); st = con.createStatement(); ResultSet rs1 = st.executeQuery("Select * from book"); while (rs1.next()) { int id = rs1.getInt(1); String title = rs1.getString(2); String author = rs1.getString(3); String publisher = rs1.getString(4); int price = rs1.getInt(5); System.out.println(id + "," + title + "," + author + "," + publisher + "," + price); } } catch (SQLException ex) { throw new Error("Error ", ex); } finally { try { if (con != null) { con.close(); } } catch (SQLException ex) { System.out.println(ex.getMessage()); } } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter