Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author tavish
- */
- import java.sql.*;
- import java.util.Properties;
- import java.util.Vector;
- public class DbOps
- {
- public Connection c;
- public ResultSet rs;
- public Vector row, columns,data;
- public void table(String q) throws SQLException
- {
- Statement stmt = null;
- try
- {
- stmt = c.createStatement();
- rs = stmt.executeQuery(q);
- } catch (SQLException e)
- {
- System.out.println("Error in db "+e.getMessage());
- }
- //rs.next();
- ResultSetMetaData md = rs.getMetaData();
- int columnCount = md.getColumnCount();
- columns = new Vector(columnCount);
- //store column names
- for (int i = 1; i <= columnCount; i++)
- {
- columns.add(md.getColumnName(i));
- }
- data = new Vector();
- //store row data
- while (rs.next())
- {
- row = new Vector(columnCount);
- for (int i = 1; i <= columnCount; i++)
- {
- row.add(rs.getString(i));
- }
- data.add(row);
- }
- }
- /**
- * Result of the query is stored in rs
- * ex: while (rs.next()) {
- * System.out.println("manager name:"+rs.getString("manager_name"));
- * }
- * @param query
- * @throws SQLException
- */
- public void query(String q) throws SQLException
- {
- Statement stmt = null;
- try
- {
- stmt = c.createStatement();
- rs = stmt.executeQuery(q);
- } catch (SQLException e)
- {
- System.out.println("Error in db "+e.getMessage());
- }
- }
- public int update(String q) throws SQLException
- {
- Statement stmt = null;
- try
- {
- stmt = c.createStatement();
- return stmt.executeUpdate(q);
- } catch (SQLException e)
- {
- System.out.println("Error in db "+e.getMessage());
- return 1;
- }
- }
- DbOps() throws ClassNotFoundException, SQLException
- {
- String dbClassName = "com.mysql.jdbc.Driver";
- String CONNECTION = "jdbc:mysql://tavish.homeip.net:3306/emotherearth";
- Class.forName(dbClassName);
- // Properties for user and password
- Properties p = new Properties();
- p.put("user", "tavish");
- p.put("password", "morpheus");
- // Now try to connect
- c = DriverManager.getConnection(CONNECTION, p);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement