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:
Here's the output:
Here's the code:
Code:
public static void main(String[] args) {
// TODO code application logic here
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try
{
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName).newInstance();
String username = "root";
String password = "password";
String url = "jdbc:mysql://localhost:3306";
conn = DriverManager.getConnection(url, username, password);
System.out.println("Creating database");
PreparedStatement ps = null;
String createDatabase = "CREATE DATABASE dbNetBeans";
ps = conn.prepareStatement(createDatabase);
ps.executeUpdate();
System.out.println("Creating table tblNetBeans");
String createTable = "CREATE TABLE dbNetBeans.tblTest (testCol varchar(30))";
ps = conn.prepareStatement(createTable);
ps.executeUpdate();
System.out.println("Inserting data to tblTest");
String insert = "INSERT INTO dbNetBeans.tblTest (testCol) VALUES ('Test info')";
ps = conn.prepareStatement(insert);
ps.executeUpdate();
ps.executeUpdate(); //two records
System.out.println("Getting info from tblTest");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM dbNetBeans.tblTest");
//rs = stmt.getResultSet(); this didn't help
System.out.println("Number of records in rs: " + rs.getFetchSize());
}
catch (ClassNotFoundException e)
{
System.out.println("Could not find class");
e.printStackTrace();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try { conn.close(); stmt.close(); }
catch (Exception ignore) { }
}
}
Code:
init: deps-jar: Compiling 1 source file to /home/steve/DatabaseTest/build/classes compile: Creating database Creating table tblNetBeans Inserting data to tblTest Getting info from tblTest Number of records in rs: 0 debug: BUILD SUCCESSFUL (total time: 3 seconds)
Comment