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)
alafift
0
Newbie Poster
Recommended Answers
Jump to PostFirst 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.?
All 2 Replies
DJSAN10
28
Posting Whiz in Training
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.