473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

ResultSet not retrieving any records

When I run this simple program I am not getting any data back. The program creates a database, creates a table, populates the table with some data, and then tries to read the data. However, no records are returned in the ResultSet. I verify through MySQL Query Browser that the database is created and populated. I've also been unable to retrieve records with other databases. Because I'm creating the database at the root I don't think admin privileges is the problem. I'm writing the program in NetBeans 5.5.1 (although the same problem happens in Eclipse 3.3), MySQL 5.0.26, MySQL Connector/J 5.0.7 on Ubuntu 7.04 (Fiesty Fawn).

Here's the code:
Expand|Select|Wrap|Line Numbers
  1.  public static void main(String[] args) {
  2.         // TODO code application logic here
  3.            Connection conn = null;
  4.            Statement stmt = null;
  5.            ResultSet rs = null;
  6.  
  7.            try
  8.            {
  9.                 String driverName = "com.mysql.jdbc.Driver"; 
  10.                 Class.forName(driverName).newInstance();
  11.                 String username = "root";
  12.                 String password = "password";
  13.                 String url = "jdbc:mysql://localhost:3306"; 
  14.                 conn = DriverManager.getConnection(url, username, password);
  15.  
  16.                 System.out.println("Creating database");
  17.                 PreparedStatement ps = null;
  18.                 String createDatabase = "CREATE DATABASE dbNetBeans";
  19.                 ps = conn.prepareStatement(createDatabase);
  20.                 ps.executeUpdate();
  21.  
  22.                 System.out.println("Creating table tblNetBeans");
  23.                 String createTable = "CREATE TABLE dbNetBeans.tblTest (testCol varchar(30))";
  24.                 ps = conn.prepareStatement(createTable);
  25.                 ps.executeUpdate();
  26.  
  27.                 System.out.println("Inserting data to tblTest");
  28.                 String insert = "INSERT INTO dbNetBeans.tblTest (testCol) VALUES ('Test info')";
  29.                 ps = conn.prepareStatement(insert);
  30.                 ps.executeUpdate();
  31.                 ps.executeUpdate(); //two records
  32.  
  33.                 System.out.println("Getting info from tblTest");
  34.                 stmt = conn.createStatement();
  35.                 rs = stmt.executeQuery("SELECT * FROM dbNetBeans.tblTest");
  36.                 //rs = stmt.getResultSet();  this didn't help
  37.                 System.out.println("Number of records in rs: " + rs.getFetchSize());
  38.  
  39.            }
  40.            catch (ClassNotFoundException e) 
  41.            {
  42.                 System.out.println("Could not find class");
  43.                 e.printStackTrace();
  44.             } 
  45.             catch (SQLException e) 
  46.             {
  47.                 System.out.println(e.getMessage());
  48.                 e.printStackTrace();
  49.             }
  50.            catch (Exception ex)
  51.             {
  52.                 ex.printStackTrace();
  53.             }
  54.            finally
  55.            {
  56.                try { conn.close(); stmt.close(); }
  57.                catch (Exception ignore) { }
  58.            }
  59.     }
  60.  
Here's the output:
Expand|Select|Wrap|Line Numbers
  1. init:
  2. deps-jar:
  3. Compiling 1 source file to /home/steve/DatabaseTest/build/classes
  4. compile:
  5. Creating database
  6. Creating table tblNetBeans
  7. Inserting data to tblTest
  8. Getting info from tblTest
  9. Number of records in rs: 0
  10. debug:
  11. BUILD SUCCESSFUL (total time: 3 seconds)
  12.  
Sep 29 '07 #1
2 2155
Oops - I'm running Java 1.6.
Sep 29 '07 #2
Never mind -- I misunderstood the purpose of getFetchSize. I thought it was similar to .Net's DataSet.Table.Rows.Count.
Oct 4 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jonathan Beam | last post by:
I'm trying to create a wrapper procedure. The procedure I'm calling returns 2 resultsets. I really only need the second, but I'm not sure how to get the second resultset. Is there something like...
0
by: Mark Jay Johansen | last post by:
Does anyone know what the relative performance would be of using updateable result sets versus using individual insert or update statements? (Specifically at the moment I'm dealing with JSP...
1
by: Catherine | last post by:
I have several ResultSet Querys/Statements within my page. An example of the code looks like this: ResultSet rs1 = stmt1.executeQuery("SELECT right(' ' + '$' +...
0
by: guru | last post by:
I want to send the whole resultset i retrieved. now i am retrieving the resultset and able to print 15 records in one frame. if i want to print whole the dump records what i have to do.? please...
2
by: Twan Kennis | last post by:
Question: How do I pass a returning resultset from a nested Stored Procedure (which opens a cursor including option "WITH RETURN TO CALLER") as a returning resultset from it's own? When I...
3
by: Augustin Prasanna | last post by:
I need to display some 200 records (dynamic) on the screen.. I need to display records from 2 tables (one to many relationship) output would look like Customer Id Name Phone NO 1 abc ...
6
by: joerg | last post by:
Hello world, my program connects to a db2 database and needs to find out the column names and data types for a specific table. The code works fine with mysql and derby, but returns an empty...
13
by: ajos | last post by:
hi frnds, im doing some db related operations in my web project,im using mysql 5.0,i have the following piece of code in my action class- for(resultset =...
3
by: John | last post by:
Hi I have an sql statement in a string variable SQLStr and I also have an oledbconnection Conn with my database. What can I do from here to run the sql and get a handle to the resultset in my...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.