Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. /**
  2.  *
  3.  * @author tavish
  4.  */
  5. import java.sql.*;
  6. import java.util.Properties;
  7. import java.util.Vector;
  8.  
  9. public class DbOps
  10. {
  11.    
  12.     public Connection c;
  13.     public ResultSet rs;
  14.     public Vector row, columns,data;
  15.    
  16.     public void table(String q) throws SQLException
  17.     {
  18.         Statement stmt = null;
  19.         try
  20.         {
  21.             stmt = c.createStatement();
  22.             rs = stmt.executeQuery(q);
  23.         } catch (SQLException e)
  24.         {
  25.             System.out.println("Error in db "+e.getMessage());
  26.         }
  27.        
  28.         //rs.next();
  29.        
  30.         ResultSetMetaData md = rs.getMetaData();
  31.         int columnCount = md.getColumnCount();
  32.  
  33.  
  34.         columns = new Vector(columnCount);
  35.  
  36.         //store column names
  37.         for (int i = 1; i <= columnCount; i++)
  38.         {
  39.             columns.add(md.getColumnName(i));
  40.         }
  41.         data = new Vector();
  42.        
  43.         //store row data
  44.         while (rs.next())
  45.         {
  46.             row = new Vector(columnCount);
  47.             for (int i = 1; i <= columnCount; i++)
  48.             {
  49.                 row.add(rs.getString(i));
  50.             }
  51.             data.add(row);
  52.         }
  53.     }
  54.    
  55.     /**
  56.      * Result of the query is stored in rs
  57.      * ex: while (rs.next()) {
  58.      *          System.out.println("manager name:"+rs.getString("manager_name"));
  59.      *      }
  60.      * @param query
  61.      * @throws SQLException
  62.      */
  63.     public void query(String q) throws SQLException
  64.     {
  65.         Statement stmt = null;
  66.         try
  67.         {
  68.             stmt = c.createStatement();
  69.             rs = stmt.executeQuery(q);
  70.         } catch (SQLException e)
  71.         {
  72.             System.out.println("Error in db "+e.getMessage());
  73.         }
  74.     }
  75.    
  76.     public int update(String q) throws SQLException
  77.     {
  78.         Statement stmt = null;
  79.         try
  80.         {
  81.             stmt = c.createStatement();
  82.             return stmt.executeUpdate(q);
  83.         } catch (SQLException e)
  84.         {
  85.             System.out.println("Error in db "+e.getMessage());
  86.             return 1;
  87.         }
  88.     }
  89.  
  90.     DbOps() throws ClassNotFoundException, SQLException
  91.     {
  92.         String dbClassName = "com.mysql.jdbc.Driver";
  93.         String CONNECTION = "jdbc:mysql://tavish.homeip.net:3306/emotherearth";
  94.  
  95.         Class.forName(dbClassName);
  96.         // Properties for user and password
  97.         Properties p = new Properties();
  98.         p.put("user", "tavish");
  99.         p.put("password", "morpheus");
  100.         // Now try to connect
  101.         c = DriverManager.getConnection(CONNECTION, p);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement