Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.03 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package miscellaneous;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.sql.ResultSet;
  13. import java.sql.ResultSetMetaData;
  14. import java.sql.Types;
  15.        
  16. /**
  17.  *
  18.  * @author andrea */
  19. public class ConnectionToDB {
  20.    
  21.     public static void main(String[] args) {
  22. /*
  23.         try {
  24.                 // MySQL Database
  25.                 Class.forName("com.mysql.jdbc.Driver"); //.newInstance();
  26.                 System.out.println("Driver for the MySQL DBMS installed!");
  27.                 String sourceURL = "jdbc:mysql://localhost:3306/andrea_db";
  28.                 //String sourceURL = "jdbc:mysql://localhost:3306/anne";
  29.                 // Create a connection through the DriverManager
  30.                 Connection databaseConnection = DriverManager.getConnection(sourceURL,"root","");
  31.                 System.out.println("Connection to the MySQL DBMS established with " + databaseConnection);
  32.                 Statement statement = databaseConnection.createStatement();
  33.                 ResultSet entryDB = statement.executeQuery("SELECT name, tel FROM contacts");
  34.                 //ResultSet entryDB = statement.executeQuery("SELECT * FROM adresse");
  35.                 // Output the resultset data
  36.                 while (entryDB.next()) {
  37.                     System.out.println(entryDB.getString("name") + " " +
  38.                                    entryDB.getString("tel"));
  39.                                    //entryDB.getString("id") + "\n" +
  40.                                    //entryDB.getString("street") + " " + entryDB.getString("number") + "\n" +
  41.                                    //entryDB.getString("zip") + " " + entryDB.getString("city") + "\n\n--\n\n");
  42.                 }
  43.                 entryDB.close();
  44.                 statement.close();
  45.                 databaseConnection.close();
  46.                 System.out.println("Closing connection to the MySQL DBMS!");
  47.                
  48.         } catch (Exception ex) {
  49.             System.out.println("JDBC Driver error");
  50.             ex.printStackTrace();
  51.         }
  52. */
  53.         // Load the driver for a MS Access Database
  54.         try {
  55.             // to include the driver to use, one can load the driver explicitly
  56.             // by calling the static forName() method in the Class class and
  57.             // passing a String object as an argument containing the driver class name.
  58.             // Load the driver class:
  59.             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  60.             System.out.println("Driver for the MS Access DBMS installed!");
  61.             // Define the data source for the driver
  62.             // JDBC uses URLs to identify the locations of both drivers and
  63.             // data sources. JDBC URLs have the following format:
  64.             // jdbc:subprotocol:data_source_identifier
  65.             // The scheme jdbc indicates that the URL refers to a JDBC data source.
  66.             // The sub-protocol identifies which JDBC driver to use. For example,
  67.             // the JDBC-ODBC Bridge uses the driver identifier odbc.
  68.             String sourceURL = "jdbc:odbc:dbdemos";
  69.             // Create a connection through the DriverManager
  70.             Connection databaseConnection = DriverManager.getConnection(sourceURL);
  71.             System.out.println("Connection to the MS Access DBMS established with " + databaseConnection);
  72.            
  73.             // A java.sql.Statement object is an object of a class that implements
  74.             // the Statement interface. When a Statement object is created, it provides
  75.             // a workspace to create an SQL query, execute it, and retrieve any results
  76.             // that are returned
  77.             Statement statement = databaseConnection.createStatement();
  78.             // The results of executing an SQL query are returned in the form of
  79.             // an object that implements the ResultSet interface and contains the
  80.             // table produced by the SQL query. The ResultSet object contains
  81.             // something called a cursor that you can manipulate to refer to any
  82.             // particular row in the resultset. This initially points to a position
  83.             // immediately preceding the first row
  84.             ResultSet entryDB = statement.executeQuery("SELECT name, capital FROM country where continent = 'North America'");
  85.             // Output the resultset data
  86.             int idx = 0;
  87.             while (entryDB.next()) {
  88.                 // Process the row
  89.                 System.out.println( (++idx) + " - " + entryDB.getString("name") + " " +
  90.                                    entryDB.getString("capital"));    
  91.             }
  92.            
  93.             // The getMetaData() method for a ResultSet object returns a reference
  94.             // to an object of type java.sql.ResultSetMetaData that encapsulates
  95.             // the metadata for the resultset
  96.             ResultSetMetaData metadata = entryDB.getMetaData();
  97.      
  98.             int columns = metadata.getColumnCount(); // Get number of columns
  99.             System.out.println("Catalog name: " + metadata.getCatalogName(1) +
  100.                                "     Column 1 name: " + metadata.getColumnName(1) +
  101.                                "     Table name: " + metadata.getTableName(1));
  102.             System.out.println("Columns in table: " + columns);
  103.             for(int i = 1 ; i<= columns ; i++) { // For each column
  104.                 if (metadata.getColumnType(i) == Types.VARCHAR) { // if it is CHAR
  105.                     System.out.println(metadata.getColumnName(i)); // display the name
  106.                 }
  107.             }
  108.          
  109.             entryDB.close();
  110.             statement.close();
  111.             databaseConnection.close();
  112.             System.out.println("Closing connection to the MS Access DBMS!");
  113.  
  114.         } catch(ClassNotFoundException cnfe) {
  115.             System.err.println(cnfe);
  116.         } catch(SQLException sqle) {
  117.             System.err.println(sqle);
  118.         }
  119.  
  120.     }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement