Advertisement
Guest User

Untitled

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