错误:找不到符号PreparedStatement

时间:2016-05-19 08:32:00

标签: java sql prepared-statement symbols

我一直收到以下错误:

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

这是我使用

的代码
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();

  }
}

我想查找“#34; Apples"在数据库中使用名为" word"。

的变量

有人能告诉我我做错了吗?

3 个答案:

答案 0 :(得分:0)

更改

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

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

并且不要在try catch块中添加Class.forName("org.postgresql.Driver"); Connection con=DriverManager...。 希望它有所帮助:)

答案 1 :(得分:0)

正如@ Shail016所说,你的名字是Connection 下面带有一些额外的try-catch块的固定代码

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

答案 2 :(得分:0)

由于没有人使用try-with-resource,这是如何使用autocloseable类来完成的。 (如果有人可以在Java 7或更高版本上测试它,我在这里工作1.6 ...)

 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
    }
}

这将在使用后自动关闭connectionm preparedStatement和resultset。这是一个很好的approch