-2

why it error cant find symbol in

PreparedStatement ps = connection.prepareStatement(sql);
                           ^
variable connection

but variable connection has benn declaredlike this : *i input the correct username, password and url

try {
        Class.forName("org.postgresql.Driver");
}
catch (java.lang.ClassNotFoundException e) {
    System.out.println(e.getMessage());
}
try {
    String url = "jdbc:postgresql://stampy.db.elephantsql.com/";
    String username = "";
    String password = "";
    Connection connection = DriverManager.getConnection(url, username, password);
    List<AdvDetail> list = new ArrayList();
} catch(SQLException e) {
        System.out.println(e.getMessage());
}

I have import library

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.*;

import a1.kmita.com.model.Adv;
import a1.kmita.com.model.AdvDetail;
import a1.kmita.com.listener.ExceptionListener;
import a1.kmita.com.model.Response;   

and the other spring libraries

here the code

try {
    String sql = "INSERT INTO advert (ad_title, ad_desc, ad_price, ad_image1,) VALUES (?, ?, ?, ?)"; /* this is the query */
    PreparedStatement ps =  connection.prepareStatement(sql); /* this is error */                
    ps.setString(1, product_name);
    ps.setString(2, product_desc);
    ps.setInt(3, product_price);
    ps.setString(4, image1);
    ps.executeUpdate();
    ps.close();         
} catch(SQLException e) {
    System.out.println(e.getMessage());
}
Alexey Subach
  • 11,903
  • 7
  • 34
  • 60

2 Answers2

0

You have not created a connection object before using it. Check this sample:

Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(DB_URL,USERNAME,PASS);
Vinit Sarvade
  • 750
  • 6
  • 14
0

connection is a varaiable, which must be initialized with your connection properties. Importing the package alone is not enough.

You could do it like so (for example for postgresql):

Connection connection = null;
try{
    Class.forName("org.postgresql.Driver");
    String connectionString = "jdbc:postgresql://" + dbhost + ":" + dbport + "/" + dbname;
    connection = DriverManager.getConnection(connectionString, dbuser, dbpassword)
}catch (Exception e){
    //...
}

Edit: The declaration of the variable must be before the try block, else the variable is in the scope of the try block. Loook at my example, how you can do this.

Fabian N.
  • 1,221
  • 10
  • 18
  • i alredy did initialized but i dont write its code on question above, sorry – Al Pepa Indra Mar 26 '17 at 17:18
  • As in the comments said, look at this list http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean and check, which point is true for you. I guess it is spelling (As you don't show us this part of the code, this is only a guess) You can post this part of the code and blank out Username/Password) – Fabian N. Mar 26 '17 at 17:26
  • sorry for not show the other part of code, but i already edit and add the other part of code. and thanks for the link you send – Al Pepa Indra Mar 26 '17 at 17:30