Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.soaa.services;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.sql.ResultSet;
- public class TestJDBC {
- public static void main(String[] args){
- Statement stmt = null;
- ResultSet rs = null;
- Connection conn = null;
- try {
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- } catch (InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- conn =
- DriverManager.getConnection("jdbc:mysql://localhost/dbbib?" +
- "user=root&password=kevin");
- stmt = conn.createStatement();
- rs = stmt.executeQuery("SELECT * FROM articles");
- // or alternatively, if you don't know ahead of time that
- // the query will be a SELECT...
- if (stmt.execute("SELECT * FROM articles")) {
- rs = stmt.getResultSet();
- }
- rs.next();
- System.out.println(rs.getString(1));
- System.out.println(rs.getString(2));
- System.out.println(rs.getString(3));
- // Now do something with the ResultSet ....
- }
- catch (SQLException ex){
- // handle any errors
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- }
- finally {
- // it is a good idea to release
- // resources in a finally{} block
- // in reverse-order of their creation
- // if they are no-longer needed
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) { } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) { } // ignore
- stmt = null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement