Spectre6 Spectre6 - 1 year ago 74
Java Question

java.sql import not working

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");
}
}

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download