Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC using ResourceBundle Class Problem

 
Nirlep Patel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

I am facing ClassNotFoundException problem

I am using NetBeans 5.5 IDE and using ResourceBunlde Class.

My code is: Main.java

package resobundledb;
import java.sql.*;
import java.util.*;

public class Main {

public static void main(String argv[]) {
Connection con = null;
//ResultSet rs;
ResourceBundle bundle = ResourceBundle.getBundle("dbresource");
System.out.println("Hello this is resourcebundle database connectivity 01");
System.out.println();

try{

String url = bundle.getString("URL");
System.out.println("url is: " + url);
Statement stmt;
ResultSet rs;

System.out.println("Driver is: " + bundle.getString("Driver"));
/* Load the Driver Class. */
Class.forName(bundle.getString("Driver"));
//Class.forName(java.util.ResourceBundle.getBundle("resobundledb/dbresource").getString("Driver"));

System.out.println("Hello this is resourcebundle database connectivity 02");
//here is where the connection is made
con = DriverManager.getConnection(url, java.util.ResourceBundle.getBundle("resobundledb/dbresource").getString("root"), java.util.ResourceBundle.getBundle("resobundledb/dbresource").getString("password"));
//con = DriverManager.getConnection(url, "root", "password");
System.out.println("Hello this is resourcebundle database connectivity 03");
stmt = con.createStatement();

//rs = stmt.executeQuery(java.util.ResourceBundle.getBundle("resobundledb/dbresource").getString("select_*_from_tbluser"));
rs = stmt.executeQuery("select * from tbluser");

System.out.println("This is the result: ");
System.out.println();

while(rs.next()){
//int uid = rs.getInt("uid");
int uid = rs.getInt(1);

//String username = rs.getString("username");
String username = rs.getString(2);

//String name = rs.getString("name");
String name = rs.getString(4);

//String bdate = rs.getString("birthdate");
String bdate = rs.getString(5);

//double sscper = rs.getDouble("sscpercentage");
double sscper = rs.getDouble(6);

//double fees = rs.getDouble("fees");
double fees = rs.getDouble(7);

//String lastaccesstime = rs.getString("lastaccessedtime");
String lastaccesstime = rs.getString(8);

System.out.println("User id: " + uid);
System.out.println("User Name: " + username);
System.out.println("Name: " + name);
System.out.println("Birth Date: " + bdate);
System.out.println("SSC Percentage: " + sscper + " %");
System.out.println("Fees: " + "Rs. " + fees);
System.out.println("Last Accessed Time: " + lastaccesstime);
}
stmt.close();
}
catch( SQLException e ) {
System.out.println("it is printing from SQLException");
e.printStackTrace();
}
catch(ClassNotFoundException e){
System.out.println("it is printing from ClassNotFoundException");
System.out.println(e.getMessage());
}
finally {
if( con != null ) {
try { con.close(); }
catch( Exception e ) { }
}
}
}
}
-----------------------------------------------------------------------------------------------------
My properties file is: dbresource.properties

Driver=com.mysql.jdbc.Driver
URL=jdbc:mysql://localhost:3306/test
select_*_from_tbluser=select * from tbluser
password=password
root=root
-----------------------------------------------------------------------------------------------------
My database name is: tbluser

and it has following fields.

userid, username, password, name, birthdate, sscpercentage, fees, lastaccessedtime


I tried a lot since from last 2 days but could not get it.

still i am trying... if someone know help me

thanks
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to divide and conquer. Firstly, your app is too long. Solve the connection issue and then look at the rest.

1) Can you prove your properties file is being found
2) can you prove the Driver string value is read?
3) Can the application find the class on the classpath?

You haven't demonstrated that you can answer any of these, and at least one is failing.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Nirlep" please check your private messages.

Dave.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic