import java.sql.*;

public class GetAllRows{
	public static void main(String[] args) {
		System.out.println("Getting All Rows from a table");
		Connection con = null;
		String url = "jdbc:mysql://localhost:80/";
		String db = "Flights";
		String driver = "com.mysql.jdbc.Driver";
		String user = "root";
		String pass = "pass";
		try{
			//Class.forName(driver).newInstance();
			con = DriverManager.getConnection(url+db, user, pass);
			try{
				Statement st = con.createStatement();
				ResultSet res = st.executeQuery("SELECT * FROM  flight");
				//System.out.println(" " + "\t" + " ");
				while (res.next()) {
					int i = res.getInt("Flno");
					String s = res.getString("ffrom");
					String a = res.getString("tto");
					String b = res.getString("distance");
					String c = res.getString("departs");
					String d = res.getString("arrives");
					System.out.println(i + "\t\t" + s + "\t\t" + a + "\t\t" + b + "\t\t" + c + "\t\t" + d);
				}
				con.close();
			}
			catch (SQLException s){
				System.out.println("SQL code does not execute.");
			}		
		}
		catch (Exception e){
			e.printStackTrace();
		}
	}
}

// **Output** 
//Getting All Rows from a table
//java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:80/Flights
	//at java.sql.DriverManager.getConnection(Unknown Source)
	//at java.sql.DriverManager.getConnection(Unknown Source)
	//at GetAllRows.main(GetAllRows.java:15)

Edited 4 Years Ago by ~s.o.s~: Added code tags, learn to use them.

First of all, use code tag when you post your code.
Secondly, format your code.
And as for your exception, you need to add the jar file of your driver to the path.
What IDE are you using? Or are you doing it via command line.?

Edited 4 Years Ago by DJSAN10: n/a

And as for your exception, you need to add the jar file of your driver to the path.

Nope. Or, at least, not yet. That error means that Java can not find any loaded driver that matches the provided url, not that java can not load the class, but rather, that it doesn't know what class to use.

@OP, un-comment the "forName" line before the "getConnection". And, if that then leads to a "ClassNotFound", etc, exception, then add the jar to the classpath.

Edit: P.S. are you sure your DB is listening on port 80 as the used url suggest? Somehow, I doubt that.

Edited 4 Years Ago by masijade: n/a

This article has been dead for over six months. Start a new discussion instead.