Advertisement
Guest User

MySQL Select WHERE AND OR LIMIT

a guest
Mar 26th, 2018
1,033
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. package net.hitmanit.sqlite;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.util.ArrayList;
  8.  
  9. public class Drive {
  10.  
  11.     public static void main(String[] args) throws Exception {
  12.         conn();//Connects to the database
  13.         ArrayList<String> array = select();
  14.         //Prints array.
  15.         for(int i = 0; i<array.size(); i++){
  16.             System.out.println(array.get(i));          
  17.         }
  18.        
  19.     }
  20.     //Make connection...
  21.     public static Connection conn(){
  22.         try{
  23.             String driver = "com.mysql.jdbc.Driver";
  24.             String url = "jdbc:mysql://64.62.211.131:3306/sqldb1";
  25.             String username = "quser";
  26.             String pass = "mypass";
  27.             Class.forName(driver);
  28.             Connection connect = DriverManager.getConnection(url, username, pass);
  29.             //System.out.println("Connected.");
  30.            
  31.             return connect;
  32.         }
  33.         catch(Exception e){System.out.println(e); return null;}    
  34.        
  35.     }
  36.     //Select statement(s)
  37.     public static ArrayList<String> select() throws Exception{
  38.         Connection con = conn();
  39.         PreparedStatement statement = con.prepareStatement("SELECT * FROM youtube WHERE author = 'Tim'");
  40.         //PreparedStatement statement = con.prepareStatement("SELECT * FROM facebook, youtube WHERE facebook.user = youtube.user AND facebook.likes = youtube.likes");
  41.         //PreparedStatement statement = con.prepareStatement("SELECT * FROM facebook, youtube WHERE facebook.user = youtube.user OR facebook.likes = youtube.likes");
  42.        
  43.         //PreparedStatement statement = con.prepareStatement("SELECT * FROM facebook WHERE user = 'Tim' ORDER BY user ASC");
  44.         //PreparedStatement statement = con.prepareStatement("SELECT * FROM facebook WHERE user = 'Tim' ORDER BY user DESC");
  45.        
  46.         //PreparedStatement statement = con.prepareStatement("SELECT * FROM facebook WHERE user = 'Tim' LIMIT 1");//Gets only 1 record...
  47.         //PreparedStatement statement = con.prepareStatement("SELECT * FROM facebook WHERE user = 'Tim' LIMIT 1 ORDER BY user DESC");// Limits to first descending result.
  48.        
  49.         ResultSet result = statement.executeQuery();
  50.         ArrayList<String> array = new ArrayList<String>();
  51.         while (result.next()){
  52.             array.add(result.getString("youtube.url"));
  53.         }  
  54.         return array;
  55.     }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement