Guest User

Untitled

a guest
Sep 14th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.94 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package guitest;
  6. import java.sql.*;
  7. import javax.swing.JOptionPane;
  8. import javax.swing.table.DefaultTableModel;
  9. /**
  10.  *
  11.  * @author Malmo
  12.  */
  13. public class SauDatabase {
  14.    
  15.     private String input;
  16.    
  17.     Connection conn = null;
  18.     Statement stmt = null;
  19.     ResultSet rs = null;
  20.     ResultSetMetaData md = null;
  21.    
  22.    
  23.    
  24.     int col;
  25.     String sqlStmt;
  26.     String dbURL;
  27.     String username;
  28.     String password;
  29.    
  30.    
  31.    
  32.    
  33.                            
  34.    
  35.     public SauDatabase(){
  36.  
  37.        
  38.         try{
  39.             dbURL = "jdbc:mysql://localhost:3306/Sau";
  40.             username = "root";
  41.             password ="";
  42.             sqlStmt = "";
  43.             col = 0;
  44.            
  45.             Class.forName("com.mysql.jdbc.Driver");
  46.  
  47.            
  48.         }
  49.         catch (ClassNotFoundException ex)
  50.     {
  51.         System.err.println("Failed to load mysql driver");
  52.         System.err.println(ex);
  53.     }
  54.        
  55.        
  56.     }
  57.    
  58.    
  59.    
  60.  
  61.     public DefaultTableModel returDTM() throws SQLException{
  62.        
  63.         DefaultTableModel tableLol = null;
  64.         tableLol = new DefaultTableModel(tablePrintOfDatabase(), getTableColumnNames());
  65.  
  66.        
  67.        
  68.        
  69.         return tableLol;
  70.     }
  71.    
  72.    
  73.     public void executeQuery(String query){
  74.        
  75.         try{
  76.             conn = DriverManager.getConnection(dbURL,username,password);
  77.             stmt = conn.createStatement();
  78.            
  79.            
  80.             stmt.execute(query);
  81.         }
  82.         catch(SQLException ex){
  83.             System.out.println("SQLException: " + ex.getMessage());
  84.             System.out.println("SQLState: " + ex.getSQLState());
  85.             System.out.println("VendorError: " + ex.getErrorCode());
  86.         }
  87.         finally
  88.         {
  89.             if(stmt != null){
  90.                 try{
  91.                     stmt.close();
  92.                 }
  93.                 catch(SQLException ex){
  94.                     /* ignore */
  95.                 }
  96.                 stmt = null;
  97.             }
  98.             if(conn != null){
  99.                 try{
  100.                     conn.close();
  101.                 }
  102.                 catch (SQLException ex){
  103.                     /*ignore*/
  104.                 }
  105.                 conn = null;
  106.             }
  107.         }
  108.        
  109.     }
  110.    
  111.    
  112.     public String getInput(){
  113.         return sqlStmt;
  114.     }
  115.    
  116.     public void setInput(String input){
  117.         sqlStmt = input;
  118.     }
  119.    
  120.    
  121.     public Object[][] tablePrintOfDatabase() throws SQLException{
  122.         Connection localConn = null;
  123.         Statement localStmt = null;
  124.         ResultSet localRs = null;
  125.         ResultSetMetaData localMd = null;
  126.         int rowCounter = 0;
  127.        
  128.         localConn = DriverManager.getConnection(dbURL,username,password);
  129.         localStmt = localConn.createStatement();
  130.         localRs = localStmt.executeQuery("SELECT * FROM sauInfo");
  131.         localMd = localRs.getMetaData();
  132.        
  133.         Object[][] db_Data = new Object[getRows()][localMd.getColumnCount()];
  134.        
  135.         while(localRs.next()){
  136.             db_Data[rowCounter][0] = localRs.getString(1);
  137.             db_Data[rowCounter][1] = localRs.getString(2);
  138.             db_Data[rowCounter][2] = localRs.getString(3);
  139.             db_Data[rowCounter][3] = localRs.getString(4);
  140.             rowCounter++;
  141.            
  142.         }
  143.        
  144.         localConn.close();
  145.         localStmt.close();
  146.         localRs.close();
  147.         localMd = null;
  148.  
  149.         return db_Data;
  150.     }
  151.    
  152.     public String[] getTableColumnNames() throws SQLException{
  153.         String[] colNames;
  154.         Connection localConn = null;
  155.         Statement localStmt = null;
  156.         ResultSet localRs = null;
  157.         ResultSetMetaData localMd = null;
  158.        
  159.         localConn = DriverManager.getConnection(dbURL,username,password);
  160.         localStmt = localConn.createStatement();
  161.         localRs = localStmt.executeQuery("SELECT * FROM sauInfo");
  162.         localMd = localRs.getMetaData();
  163.                        
  164.         colNames = new String[localMd.getColumnCount()];
  165.        
  166.         for(int i = 0 ; i < colNames.length; i++){
  167.             colNames[i] = localMd.getColumnName(i+1);
  168.         }
  169.        
  170.         return colNames;
  171.        
  172.     }
  173.    
  174.    
  175.    
  176.    
  177.     /*
  178.      * IKKE I BRUK !!!!
  179.      */
  180.     /*
  181.     public void table() throws SQLException{
  182.         conn = DriverManager.getConnection(dbURL,username,password);
  183.         stmt = conn.createStatement();
  184.         rs = stmt.executeQuery("SELECT * FROM sauInfo");
  185.         md = rs.getMetaData();
  186.        
  187.         String[][] tableView = new String[getRows()][md.getColumnCount()];
  188.        
  189.         for(int i = 0; i < tableView[0].length;i++){
  190.             tableView[0][i] = md.getColumnName(i+1); //tabell fra databasen er ikke null-indeksert, starter dermed fra 1 og oppover.
  191.         }
  192.         System.out.println(tableView[0][0]+ "\t\t" + tableView[0][1]+ "\t\t" + tableView[0][2]+ "\t\t" + tableView[0][3]);
  193.         while(rs.next()){
  194.              System.out.println(rs.getString(1) + "\t\t" + rs.getString(2) + "\t\t\t" + rs.getString(3) + "\t\t" + rs.getString(4));
  195.  
  196.         }      
  197.     }
  198.    
  199.     */
  200.    
  201.    
  202.     //Henter antall rader i databasen, teller antall ID!
  203.     public int getRows() throws SQLException{
  204.         Connection localConn = null;
  205.         Statement localStmt = null;
  206.         ResultSet localRs = null;
  207.         int localRows = -1;
  208.         try{
  209.             localConn = DriverManager.getConnection(dbURL,username,password);
  210.             localStmt = localConn.createStatement();
  211.             localRs = localStmt.executeQuery("SELECT COUNT(*) FROM sauInfo;");
  212.             localRs.next();
  213.             localRows = localRs.getInt(1);
  214.         }
  215.         catch (SQLException ex){
  216.             System.out.println("SQLException: " + ex.getMessage());
  217.             System.out.println("SQLState: " + ex.getSQLState());
  218.             System.out.println("VendorError: " + ex.getErrorCode());
  219.         }
  220.          finally
  221.         {
  222.             if(localStmt != null){
  223.                 try{
  224.                     localStmt.close();
  225.                 }
  226.                 catch(SQLException ex){
  227.                     /* ignore */
  228.                 }
  229.                 localStmt = null;
  230.             }
  231.             if(localConn != null){
  232.                 try{
  233.                     localConn.close();
  234.                 }
  235.                 catch (SQLException ex){
  236.                     /*ignore*/
  237.                 }
  238.                 localConn = null;
  239.             }
  240.             if(localRs != null)
  241.             {
  242.                 try{
  243.                     localRs.close();
  244.                 }
  245.                 catch(SQLException ex){
  246.                     /*ignore*/
  247.                 }
  248.                 localRs = null;
  249.             }
  250.         }
  251.  
  252.        
  253.         return localRows;
  254.            
  255.     }
  256.  
  257.    
  258.    
  259. }
Advertisement
Add Comment
Please, Sign In to add comment