Advertisement
elsemTim

Untitled

Nov 12th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. //package clientServerApp;
  2. import java.sql.*;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.Properties;
  9.  
  10. public class FirebirdExample1 {
  11.  
  12.     /**
  13.      * @param args
  14.      */
  15.     public static void main(String[] args) {
  16.         String strDatabasePath = "d:\\DBWork\\OURDB.FDB";
  17.         String strURL="jdbc:firebirdsql://localhost:3050/"+strDatabasePath;
  18.         Properties props = new Properties();
  19.         //Наши настройки для подключения
  20.         props.setProperty("user", "SYSDBA");
  21.         props.setProperty("password", "123");
  22.         props.setProperty("encoding","UNICODE_FSS");
  23.  
  24.         // SQL запрос, который будет выполняться.
  25.         String strSQL="SELECT * from positionn";
  26.  
  27.         try
  28.         {
  29.             Class.forName("org.firebirdsql.jdbc.FBDriver");
  30.         }
  31.         catch(Exception e)
  32.         {
  33.             System.err.println("Unable to load driver: "+e);
  34.         }
  35.  
  36.         Connection conn=null;
  37.         try
  38.         {
  39.             //Создаём подключение к базе данных
  40.             //conn = DriverManager.getConnection(strURL,strUser, strPassword);
  41.             conn = DriverManager.getConnection(strURL,props);
  42.  
  43.  
  44.             if (conn==null)
  45.             {
  46.                 System.err.println("Could not connect to database");
  47.             }
  48.  
  49.             // Создаём класс, с помощью которого будут выполняться
  50.             // SQL запросы.
  51.             Statement stmt = conn.createStatement();
  52.  
  53.             //Выполняем SQL запрос.
  54.             ResultSet rs = stmt.executeQuery(strSQL);
  55.  
  56.             // Смотрим количество колонок в результате SQL запроса.
  57.             int nColumnsCount = rs.getMetaData().getColumnCount();
  58.  
  59.             // Выводим результат.
  60.             while(rs.next())
  61.             {
  62.                 System.out.println();
  63.                 for (int n=1;n < nColumnsCount+1;n++)
  64.                 {
  65.                     Object obj = rs.getObject(n);
  66.                     if (obj!=null)
  67.                     {
  68.                         System.out.print(obj+" | ");
  69.                     }
  70.                 }
  71.             }
  72.  
  73.             // Освобождаем ресурсы.
  74.             stmt.close();
  75.  
  76.             conn.close();
  77.         }
  78.         catch(SQLException ex)
  79.         {
  80.             ex.printStackTrace();
  81.         }
  82.  
  83.         System.out.print("end.");
  84.     }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement