Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.hitmanit.sqlite;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.ArrayList;
- public class Drive {
- public static void main(String[] args) throws Exception {
- conn();//Connects to the database
- ArrayList<String> array = select();
- //Prints array.
- for(int i = 0; i<array.size(); i++){
- System.out.println(array.get(i));
- }
- }
- //Make connection...
- public static Connection conn(){
- try{
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://64.62.211.131:3306/sqldb1";
- String username = "quser";
- String pass = "mypass";
- Class.forName(driver);
- Connection connect = DriverManager.getConnection(url, username, pass);
- //System.out.println("Connected.");
- return connect;
- }
- catch(Exception e){System.out.println(e); return null;}
- }
- //Select statement(s)
- public static ArrayList<String> select() throws Exception{
- Connection con = conn();
- PreparedStatement statement = con.prepareStatement("SELECT * FROM youtube WHERE author = 'Tim'");
- //PreparedStatement statement = con.prepareStatement("SELECT * FROM facebook, youtube WHERE facebook.user = youtube.user AND facebook.likes = youtube.likes");
- //PreparedStatement statement = con.prepareStatement("SELECT * FROM facebook, youtube WHERE facebook.user = youtube.user OR facebook.likes = youtube.likes");
- //PreparedStatement statement = con.prepareStatement("SELECT * FROM facebook WHERE user = 'Tim' ORDER BY user ASC");
- //PreparedStatement statement = con.prepareStatement("SELECT * FROM facebook WHERE user = 'Tim' ORDER BY user DESC");
- //PreparedStatement statement = con.prepareStatement("SELECT * FROM facebook WHERE user = 'Tim' LIMIT 1");//Gets only 1 record...
- //PreparedStatement statement = con.prepareStatement("SELECT * FROM facebook WHERE user = 'Tim' LIMIT 1 ORDER BY user DESC");// Limits to first descending result.
- ResultSet result = statement.executeQuery();
- ArrayList<String> array = new ArrayList<String>();
- while (result.next()){
- array.add(result.getString("youtube.url"));
- }
- return array;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement