0

My .getTables and .prepareStatement are not working. I thought I only had to import the java.sql.* for these to work. Please let me know what else I need to do. Thank you for your time. It says "cannot find symbol" next to both lines and will not compile.

import edu.lcc.citp.inventory.Product;
import java.sql.DriverManager;
import javax.jms.Connection;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.jms.JMSException;

public class DatabaseProductDao implements DataAccessObject<Product> {

Connection con;

public DatabaseProductDao() throws SQLException, JMSException, ClassNotFoundException {

    Class.forName("cockeb.org.apache.derby.jdbc.ClientDriver");

    try (Connection con = (Connection) DriverManager.getConnection("jdbc:derby://localhost:1527/store;create=true")) {
        boolean exists = con.getMetaData().getTables(null, null, "PRODUCT", null).next();
        if (exists) {
            System.out.println("Table Exists");
        } else {
            String createDml = "CREATE TABLE PRODUCT (UPC VARCHAR(25), SHORT_DETAILS VARCHAR(50), LONG_DETAILS VARCHAR(5000), PRICE DECIMAL(10,2), STOCK INTEGER, PRIMARY KEY (UPC))";
            PreparedStatement createStatement =     con.prepareStatement(createDml);
            createStatement.execute();
        }
    } catch (SQLException e) {
        System.out.println("Can Not Connect At This Time");
    }
}
Atul Dwivedi
  • 1,452
  • 16
  • 29
Spectre6
  • 33
  • 1
  • 2
  • 6

5 Answers5

1

You need to add the following imports

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

instead of the one you are using

anthoon
  • 101
  • 9
1

I'd suggest that you remove these lines:

import javax.jms.Connection;
import javax.jms.JMSException;

...as it's probably not the Connection class that you actually intended on importing. Your java.sql.* import should grab the correct one once you remove the lines above.

Catchwa
  • 5,845
  • 4
  • 31
  • 57
1

The problem is with imports. You imported javax.jms.Connection which is obiously wrong. Just delete it. What you wanted is Connection class from java.sql (java.sql.Connection) package.

Also I do not suggest to use wildcards (.*) in import but pick specific class you actually use. In your case:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
MartyIX
  • 27,828
  • 29
  • 136
  • 207
tmucha
  • 689
  • 1
  • 4
  • 19
0

Some of your imports are wrong. You need below to make it work.

  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.Connection;

One step ahead I would like to suggest you to have a separate class to establish the database connection. This way you don't need to repeat the same code again.

Sample code. (Do changes for this as appropriate.)

for instance have a DatabaseCon.java class in your project

  package classes;

  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;

  public class DatabaseCon {

      private static Connection c;

      private static void connect()
             throws Exception {
          Class.forName("com.mysql.jdbc.Driver").newInstance();
          String url = "db_url/db";
          c = DriverManager.getConnection(url, "username", "pass");
      }

      public static PreparedStatement prepareState(String sql)
           throws Exception {

          if (c == null) {
              connect();
          }
          return c.prepareStatement(sql);
      }

   }

This can then be called by

  public void yourMethod() {
      PreparedStatement p = null;
      try {
           p = DatabaseCon.prepareState("Your_query");
           ............

      } catch (Exception e) {
          //catch it
      } finally {
          //do the final stuff
      }
 }

Note This way is good if it is a fairly big project as you've mentioned.

Thush-Fdo
  • 506
  • 1
  • 9
  • 28
0

You have imported a few wrong classes for use.

import java.sql.DriverManager;
import javax.jms.Connection;
import java.sql.*;
import javax.jms.JMSException;

The jms imports are of no use; you have imported them in vain, which is causing issues in your program.

The main import required is java.sql.*, the application will work correctly if you just remove the jms imports.

But, the best practice to import classes is to specify the specific class from which you are using the element/method.

See here for the reason Single import vs package import

Community
  • 1
  • 1
srp321
  • 116
  • 2
  • 10