Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.90 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. public class DatabaseTest
  9. {
  10.     private static Connection connect = null;
  11.     private String sql;
  12.  
  13.     public DatabaseTest()
  14.     {
  15.     }
  16.    
  17.     /**
  18.      * Returns a connection to the mysql database
  19.      * @return Connection to Database
  20.      */
  21.     private static Connection getConnection()
  22.     {
  23.         try
  24.         {      
  25.             // This will load the MySQL driver
  26.             Class.forName("com.mysql.jdbc.Driver");
  27.             // Setup the connection with the DB
  28.             connect = DriverManager
  29.                     .getConnection("jdbc:mysql://localhost/dbTestJava?"
  30.                             + "user=root&password=");  
  31.         }
  32.         catch (Exception e)
  33.         {
  34.            
  35.             e.printStackTrace();
  36.         }
  37.        
  38.         return connect;
  39.     }
  40.    
  41.     private void terminateDatabase( Connection db )
  42.     {      
  43.         try
  44.         {
  45.             //close the connection
  46.             db.close();
  47.         }
  48.         catch( SQLException sqlE )
  49.         {
  50.             sqlE.getStackTrace();
  51.         }
  52.     }
  53.    
  54.     /**
  55.      * Creates a row in the database
  56.      */
  57.     public void createRow(String[] args)
  58.     {
  59.         Connection db = getConnection();
  60.         PreparedStatement preState;
  61.         try    
  62.         {
  63.             sql =   "INSERT INTO books (title, author) VALUES (?, ?); ";
  64.            
  65.             preState    =   db.prepareStatement( sql );
  66.             preState.setString(1, args[0] );
  67.             preState.setString(2, args[1]);
  68.            
  69.             preState.executeUpdate();
  70.             //close the connection
  71.             db.close();
  72.         }
  73.         catch(SQLException e)
  74.         {
  75.             e.getStackTrace();
  76.         }
  77.         finally
  78.         {
  79.             this.terminateDatabase( db );
  80.         }
  81.        
  82.     }
  83.    
  84.     /**
  85.      * delets a row in the databas
  86.      * @param int id
  87.      */
  88.     public void deleteRow( int id )
  89.     {
  90.         Connection db = getConnection();
  91.         try {
  92.             sql =   "DELETE FROM books WHERE id=" + id;
  93.             Statement stmt  =   db.createStatement();
  94.             stmt.execute(sql);
  95.            
  96.         }
  97.         catch (SQLException e) {
  98.             // TODO Auto-generated catch block
  99.             e.printStackTrace();
  100.         }
  101.         finally
  102.         {
  103.             this.terminateDatabase( db );
  104.         }
  105.        
  106.        
  107.     }
  108.    
  109.     /**
  110.      * Updates a row
  111.      * @param int id
  112.      */
  113.     public void updateRow( int id, String[] args )
  114.     {
  115.        
  116.         Connection db = getConnection();
  117.         PreparedStatement preState;
  118.         try    
  119.         {
  120.             sql =   "UPDATE books SET title=?, author=? WHERE id=" + id;
  121.  
  122.             preState    =   db.prepareStatement( sql );
  123.             preState.setString(1, args[0] );
  124.             preState.setString(2, args[1]);
  125.            
  126.             preState.executeUpdate();
  127.         }
  128.         catch(SQLException e)
  129.         {
  130.             e.getStackTrace();
  131.         }
  132.         finally
  133.         {
  134.             this.terminateDatabase( db );
  135.         }
  136.     }
  137.    
  138.     /**
  139.      * selects a row
  140.      * @param int id
  141.      */
  142.     public void selectRow( int rowId )
  143.     {
  144.         Connection db = getConnection();
  145.         sql =   "SELECT id, title, author FROM books WHERE id=" + rowId;
  146.        
  147.         try {
  148.             Statement   stmt    =   db.createStatement();
  149.             ResultSet rs    =   stmt.executeQuery( sql );
  150.            
  151.             while ( rs.next() )
  152.             {
  153.                 System.out.println( rs.getString("author") );
  154.             }          
  155.         }
  156.         catch (SQLException e) {
  157.             // TODO Auto-generated catch block
  158.             e.printStackTrace();
  159.         }
  160.         finally
  161.         {
  162.             this.terminateDatabase( db );
  163.         }
  164.     }
  165.    
  166.     /**
  167.      * selects all rows
  168.      */
  169.     public void selectAll()
  170.     {
  171.         Connection db = getConnection();
  172.         sql =   "SELECT id, title, author FROM books";
  173.        
  174.         try {
  175.             Statement   stmt    =   db.createStatement();
  176.             ResultSet rs    =   stmt.executeQuery( sql );
  177.            
  178.             while ( rs.next() )
  179.             {
  180.                 System.out.println( rs.getString( "title" ) + " (" + rs.getString("author") + ")" );
  181.             }
  182.         }
  183.         catch (SQLException e) {
  184.             // TODO Auto-generated catch block
  185.             e.printStackTrace();
  186.         }
  187.         finally
  188.         {
  189.             this.terminateDatabase( db );
  190.         }
  191.        
  192.     }
  193.    
  194.     /**
  195.      * @param args
  196.      */
  197.     public static void main(String[] args) {
  198.         DatabaseTest    dbTest  =   new DatabaseTest();
  199.        
  200.         //String[]  val = {"telekinese", "Bpr" };
  201.         //dbTest.createRow( val );
  202.        
  203.         //dbTest.selectRow( 5 );
  204.        
  205.         //dbTest.deleteRow( 2 );
  206.        
  207.         //dbTest.selectAll();
  208.         //dbTest.updateRow(5, val);
  209.         //dbTest.selectAll();
  210.     }
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement