简体   繁体   中英

error: cannot find symbol PreparedStatement

I keep getting the following error:

error: cannot find symbol PreparedStatement st = conn.prepareStatement("SELECT * FROM table WHERE name = ?");
symbol: variable conn
location: class splitString

Here's the code I'm using

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

class splitString {
  public static void main(String[] args) {

  //Connect to database
    try {
       Class.forName("org.postgresql.Driver"); 
       Connection con = DriverManager.getConnection("jdbc:postgresql://localhost/database", "postgres", "pswd");
       if (con != null)
           System.out.println("Connection Successful!");
    } catch(Exception ee) {
        ee.printStackTrace();
    }

    String word = "Apples";
    PreparedStatement st = conn.prepareStatement("SELECT * FROM table WHERE name = ?");
    st.setString(1, word);
    ResultSet rs = st.executeQuery();

  }
}

I want to look up the word "Apples" in the database using the variable named "word".

Can someone tell me what I'm doing wrong?

Change

Connection con=DriverManager.getConnection("jdbc:postgresql://localhost/database", "postgres", "pswd");

to

Connection conn=DriverManager.getConnection("jdbc:postgresql://localhost/database", "postgres", "pswd");

and don't add Class.forName("org.postgresql.Driver"); Connection con=DriverManager... Class.forName("org.postgresql.Driver"); Connection con=DriverManager... in try catch block. hope it helps :)

As say @Shail016 you have typo in name of Connection Below fixed code with some additional try-catch blocks

package main;

import java.sql.*;

class splitString {
    public static void main(String[] args) {

        //Connect to database
        Connection con = null;
        String word = "Apples";

        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            con = DriverManager.getConnection("jdbc:postgresql://localhost/database", "postgres", "pswd");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (con != null)
            System.out.println("Connection Successful!");
        else return;

        PreparedStatement st;
        ResultSet rs = null;
        try {
            st = con.prepareStatement("SELECT * FROM table WHERE name = ?");
            st.setString(1, word);
            rs = st.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Since nobody used the try-with-resource, this is how to do it with the autocloseable class. (If someone can test it on a Java 7 or greater, I work on 1.6 here...)

 try {
    Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
    e.printStackTrace();
    return;
}

try(
    Connection con = DriverManager.getConnection("jdbc:postgresql://localhost/database", "postgres", "pswd");
    PreparedStatement st = con.prepareStatement("SELECT * FROM table WHERE name = ?");
){
    st.setString(1, word);
    try(rs = st.executeQuery();){
        //Read the resultset
    }
}

This will close the connectionm preparedStatement and the resultset automaticly after use. This is a good approch

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM