Results 1 to 3 of 3
- 05-27-2011, 06:50 PM #1
Trouble linking up to MySql database
Can anyone check this code and maybe see what I am doing wrong? I have an sql database called MyProfile and I am trying to experiment with it by selecting a field of the client's Name, Email, and amount paid. I want to get those values and system.out to the screen. All that happens is it gives me the path of the jdbc driver. Here's the code, and I welcome any suggestions :
import javax.swing.*;
import java.sql.*;
public class MyProfile
{
public static void main(String[] args)
{
new MyProfile();
}
public MyProfile()
{
try
{
Statement s = getConnection().createStatement();
String select = "Select name, email, paid from profile order by name";
ResultSet rows = s.executeQuery(select);
while (rows.next())
{
String name = rows.getString(1);
String email = rows.getString(2);
double paid = rows.getDouble(3);
System.out.println(name + email + paid);
}
}
catch (SQLException e)
{
System.out.println("Exception caught");
}
}
private static Connection getConnection()
{
Connection con = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
String url;
String user;
String pw;
url = "jdbc.mysql://localhost/MyProfile";
user = "root";
pw = "joel";
con = DriverManager.getConnection(url, user, pw);
System.out.println("Opened successfully");
}
catch (ClassNotFoundException e)
{
System.out.println(e.getMessage());
System.exit(0);
}
catch (SQLException e)
{
System.out.println(e.getMessage());
System.exit(0);
}
return con;
}
}
- 05-27-2011, 07:50 PM #2
What statement gives you that?it gives me the path of the jdbc driver.
Can you show all the output from your program?
In your catch statements add e.printStackTrace() to get full details on the exception
- 05-27-2011, 10:17 PM #3
I know, great question. It appears on the console, which is weird considering there is no output statement. I also dont think the Class.forName(string) is supposed to output anything either? The only output is on the console : com.mysql.jdbc.Driver
BTW thanks for the tips on adding the printStackTrace(), although in this case there are no exceptions thrown as it compiles just fine. I also made sure I added MySQL Connector/J (the .jar file) to the classpath.
Thanks for your help Norm. I welcome any other thoughts or suggestions.
Similar Threads
-
Trouble using JSP for MySQL database
By killasaurus tex in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 04-13-2011, 06:39 AM -
JDBC and mySQL database trouble
By ichwar in forum JDBCReplies: 17Last Post: 08-16-2010, 11:21 PM -
Trouble with jtables from a database
By mitty in forum AWT / SwingReplies: 1Last Post: 04-20-2010, 03:05 PM -
How to convert access database to mysql database?
By vrk in forum Advanced JavaReplies: 2Last Post: 02-11-2009, 04:43 AM -
database with mysql using the netbeans 6.0
By kwesiaryee in forum New To JavaReplies: 2Last Post: 05-02-2008, 04:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks