Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package MES;
  7.  
  8. import java.sql.*;
  9. import java.util.logging.Logger;
  10.  
  11. /**
  12.  *
  13.  * @author Morten
  14.  */
  15. public class ERP_Connect {
  16.  
  17.     private Connection con = null;
  18.  
  19.     public static void main(String[] args) {
  20. //        ERP_Connect obj = new ERP_Connect();
  21. //        obj.run(args);
  22.         ERP_Connect obj2 = new ERP_Connect();
  23.         Connection conn = obj2.getConnection();
  24.         try {
  25.             Statement stmt = conn.createStatement();
  26.             stmt.execute("SELECT ITEMID, NAME, QTYCALC, SCHEDSTART, SCHEDEND FROM PRODTABLE WHERE DATAAREAID = 't074'");
  27.             ResultSet rs = stmt.getResultSet();
  28.             ResultSetMetaData rsmd = rs.getMetaData();
  29.             int columnsNumber = rsmd.getColumnCount();
  30.             while (rs.next()) {
  31.                 for (int i = 1; i <= columnsNumber; i++) {
  32.                     if (i > 1) System.out.print(",  ");
  33.                     String columnValue = rs.getString(i);
  34.                     System.out.print(columnValue + " " + rsmd.getColumnName(i));
  35.                 }
  36.                 System.out.println("");
  37.             }
  38.  
  39.         } catch (SQLException ex) {
  40.             //Logger.getLogger(ERP_Connect.class.getName()).log(Level.SEVERE, null, ex);
  41.         }
  42.     }
  43.  
  44.     public Connection getConnection() {
  45.  
  46.         try {
  47.             // The atributes of the server
  48.             //  jdbc:sqlserver:[localhost[\instanceName][:portNumber]][;property=value[;property=value]];
  49.             String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // NetDirect JDBC driver
  50.             String serverName = "10.137.0.21";
  51.             String portNumber = "1433";
  52.             String mydatabase = serverName + ":" + portNumber + ";databaseName=Dynamics09";
  53.             String url = "jdbc:sqlserver://" + mydatabase + ";;user=AXReader;password=AXReader"; // a JDBC url
  54.             System.out.println("Connecting to database... " + "(" + url + ")");
  55.            
  56.             try {
  57. // Load the JDBC driver
  58.                 Class.forName(driverName);
  59.             } catch (ClassNotFoundException e) {
  60.                 System.out.println(e);
  61.             }
  62.  
  63. // Create a connection to the database
  64.             DriverManager.setLoginTimeout(1);
  65.             con = DriverManager.getConnection(url);
  66.  
  67.         } catch (SQLException e) {
  68.  
  69.             System.err.println("No connection to database... Using hardcoded values instead!");
  70.             System.out.println(e);
  71. // Could not find the database driver
  72.         }
  73.        
  74.         return con;
  75.  
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement