Advertisement
Guest User

Untitled

a guest
Jun 14th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.70 KB | None | 0 0
  1. /*
  2.  *      DBTest.java
  3.  *      
  4.  *      Copyright 2008 Robert Ketteringham <robket@robket-desktop>
  5.  *      
  6.  *      This program is free software; you can redistribute it and/or modify
  7.  *      it under the terms of the GNU General Public License as published by
  8.  *      the Free Software Foundation; either version 2 of the License, or
  9.  *      (at your option) any later version.
  10.  *      
  11.  *      This program is distributed in the hope that it will be useful,
  12.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *      GNU General Public License for more details.
  15.  *      
  16.  *      You should have received a copy of the GNU General Public License
  17.  *      along with this program; if not, write to the Free Software
  18.  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19.  *      MA 02110-1301, USA.
  20.  */
  21.  
  22. //export CLASSPATH=/usr/share/java/mysql-connector-java-5.1.5.jar:$CLASSPATH
  23. //export CLASSPATH=/usr/share/java/mysql-connector-java.jar:$CLASSPATH
  24.  
  25. //Installed the database connectivity by installing the package libmysql-java...
  26. // and then copying the connecter to the jre directory (running this command)
  27. // sudo cp /usr/share/java/mysql-connector-java-5.1.5.jar /usr/lib/jvm/java-6-sun-1.6.0.03/jre/lib/ext
  28.  
  29. import java.sql.*;
  30. import java.awt.*;
  31. import javax.swing.*;
  32. public class DBTest {
  33.     public static void runMe (String host, String database, String user, String password) throws Exception
  34.     {
  35.        
  36.         /* run driverTest method shown below */
  37.         driverTest();
  38.        
  39.         /* make the connection to the database */
  40.         Connection conMe = makeCon (host, database, user, password);
  41.  
  42.         /* now run a select query of the intended database */
  43.         exeUpdate(conMe, "INSERT INTO testTB VALUES (1234,'Oohh') ; INSERT INTO testTB VALUES (1235,'Dear')");
  44.         exeQuery (conMe, "SELECT * FROM testTB");
  45.        
  46.         /* close the database */
  47.         conMe.close();
  48.     }
  49.  
  50.     protected static void driverTest () throws java.lang.ClassNotFoundException
  51.     {
  52.         try
  53.         {
  54.             Class.forName("org.gjt.mm.mysql.Driver");
  55.             Class.forName("com.mysql.jdbc.Driver");
  56.             System.out.println("MySQL Driver Found");
  57.         } catch (java.lang.ClassNotFoundException e) {
  58.             System.err.println("MySQL JDBC Driver not found ... ");
  59.             throw (e);
  60.         }
  61.     }  
  62.    
  63.    protected static Connection makeCon (String host, String database, String user, String password) throws java.sql.SQLException
  64.    {
  65.         String url = "";
  66.         try
  67.         {
  68.             url = "jdbc:mysql://" + host + ":3306/" + database;
  69.             Connection con = DriverManager.getConnection(url, user, password);
  70.             System.out.println("Connection established to " + url + "...");
  71.             return con;
  72.         }
  73.         catch (java.sql.SQLException e)
  74.         {
  75.             System.out.println("Connection couldn't be established to " + url);
  76.             throw (e);
  77.         }
  78.     }
  79.    
  80.     protected static void exeQuery(Connection con, String sqlStatement)
  81.           throws SQLException {
  82.  
  83.                 try {
  84.                         Statement cs = con.createStatement();
  85.                         ResultSet sqls = cs.executeQuery(sqlStatement);
  86.  
  87.                         /*while (sqls.next()) {
  88.                                 String id = (sqls.getObject("IDNumber").toString());
  89.                                 String data = (sqls.getObject("Name").toString());
  90.                                 System.out.println(id + " " + data);
  91.                         }*/
  92.                        
  93.                             TableModel dataModel = new AbstractTableModel() {
  94.                               public int getColumnCount() { return 10; }
  95.                               public int getRowCount() { return 10;}
  96.                               public Object getValueAt(int row, int col) {
  97.                                 sqls.absolute(row);
  98.                                 return sqls.getObject(col); }
  99.                           };
  100.                           JTable table = new JTable(dataModel);
  101.                           JScrollPane scrollpane = new JScrollPane(table);
  102.  
  103.                         sqls.close();
  104.  
  105.                 } catch (SQLException e) {
  106.                         System.out.println ("Error executing sql statement");
  107.                         throw (e);
  108.                 }
  109.     }
  110.      
  111.        protected static void exeUpdate(Connection con, String sqlStatement)
  112.           throws SQLException {
  113.  
  114.                 try {
  115.                         Statement cs = con.createStatement();
  116.                         cs.executeUpdate(sqlStatement);
  117.  
  118.                 } catch (SQLException e) {
  119.                         System.out.println ("Error executing sql statement");
  120.                         throw (e);
  121.                 }
  122.     }
  123.     public static void main (String args[]) throws Exception
  124.     {      
  125.         runMe("localhost","polobob", "bob","");
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement