Advertisement
salahzar

jdbc access java part

May 17th, 2017
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.87 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.Statement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.List;
  10. import java.util.ArrayList;
  11. import java.sql.ResultSetMetaData;
  12.  
  13. public class DevJDBC {
  14.  
  15.     static Connection conn;
  16.     static PreparedStatement stmt;
  17.     static ResultSet result;
  18.     static Map<Integer, String> items= new HashMap<Integer,String>() ;
  19.     static Map<String, Double> values = new HashMap<String, Double>();
  20.     static List<PreparedStatement> stmtlist = new ArrayList<PreparedStatement>();
  21.     //static List<HashMap<Integer, String>> itemlist = new ArrayList<HashMap<Integer,String>>();
  22.  
  23.     public static boolean getConnection(String url, String user, String password) {
  24.  
  25.         try {
  26.  
  27.             //Class.forName("oracle.jdbc.driver.OracleDriver");
  28.             System.out.println("connecting the database...");
  29.             conn = DriverManager.getConnection(url, user, password);
  30.         } catch (Exception e) {
  31.             e.printStackTrace();
  32.         }
  33.  
  34.         if (conn != null) {
  35.             System.out.println("connect the database success");
  36.             return true;
  37.  
  38.         } else {
  39.             return false;
  40.         }
  41.     }
  42.     public static boolean execute(String sql) {
  43.  
  44.         try {
  45.             //System.out.println("execute sql "+sql);
  46.             Statement stat = conn.createStatement();
  47.             stat.execute(sql);
  48.             return true;
  49.         } catch (Exception e) {
  50.             e.printStackTrace();
  51.             return false;
  52.         }
  53.     }
  54.  
  55.     public static int prepareStmt(String sql) {
  56.         int index = 0 ;
  57.         try {
  58.             // System.out.println(sql);
  59.             stmt = conn.prepareStatement(sql);
  60.             stmtlist.add(stmt);
  61.         } catch (Exception e) {
  62.             e.printStackTrace();
  63.         }
  64.         if (stmt != null) {
  65.             System.out.println(sql);
  66.             index = stmtlist.size();
  67.             System.out.println("prepare sql stmt count:" + index);
  68.             return index;
  69.         } else {
  70.  
  71.             return 0;
  72.  
  73.         }
  74.  
  75.     }
  76.  
  77.     public static boolean executeQuery(int index) {
  78.         // Tag_data Tag = new Tag_data();
  79.         try {
  80.             if (stmt != null) {
  81.                 System.out.println("index of stmt:" + (index-1));
  82.                 PreparedStatement execstmt = stmtlist.get(index-1);
  83.                 result = execstmt.executeQuery();
  84.             }
  85.         } catch (Exception e) {
  86.  
  87.             e.printStackTrace();
  88.         }
  89.         if (result != null) {
  90.             System.out.println("execute query success");
  91.             return true;
  92.         } else {
  93.             System.out.println("execute query failed");
  94.             return false;
  95.         }
  96.     }
  97.  
  98.     public static void setColname(int index, String colname) {
  99.        
  100.         items.put(index, colname);
  101.         System.out.println(items);
  102.         //itemlist.add(items);
  103.         //System.out.println(itemlist);
  104.     }
  105.  
  106.     public static double readValue(int i ) {
  107.         double value = 0;
  108.         int j;
  109.         try {
  110.             ResultSetMetaData rsmd = result.getMetaData();
  111.             if (items.containsKey( i )) {
  112.                 String colname = items.get(i);
  113.                 String colname1 = colname.toUpperCase();
  114.                 while (result.next()) {
  115.                     for (j = 1; j < rsmd.getColumnCount() + 1; j++)
  116.                     {
  117.                         // System.out.println("put the value start..");
  118.                         // value[j] = result.getDouble(j);
  119.                         values.put(rsmd.getColumnName(j), result.getDouble(j));
  120.                         System.out.println("database value: " + " columnname:" + rsmd.getColumnName(j) + " value:"
  121.                                 + result.getDouble(j));
  122.                     }
  123.                 }
  124.                 if (values.containsKey(colname1)) {
  125.                     value = values.get(colname1);
  126.                     System.out.println(" read  tag value:" + " columnname:" + colname1 + "  tag value:" + value);
  127.                 }
  128.             }
  129.         } catch (Exception e) {
  130.  
  131.             e.printStackTrace();
  132.         }
  133.         return value;
  134.     }
  135.    
  136.     public static void logoff() throws SQLException {
  137.  
  138.         if (result != null)
  139.             result.close();
  140.         if (stmtlist != null)
  141.             {     for(PreparedStatement stmt:stmtlist ){
  142.                     stmt.close();
  143.                 }
  144.             }
  145.         if (conn != null)
  146.             conn.close();
  147.         items.clear();
  148.         values.clear();
  149.         stmtlist.clear();
  150.         //itemlist.clear();
  151.         System.out.println("database closed!");
  152.  
  153.     }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement