0

所以我对 java 和 SQL 很陌生,它们是我的第一门编程语言。我正在尝试使用 JDBC 做一些工作。我想允许用户输入一个 id 并返回一个基于变量的查询。如果有人至少可以指出我正确的方向......这是我开始的代码。请注意它的粗略,但只是试图获得一段工作代码,以便我可以在我的主类中更好地实现它。

 Scanner input = new Scanner(System.in);
    Class.forName("org.sqlite.JDBC");
    Connection conn =
            DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Derek\\Documents\\Databases\\example.sqlite");
    Statement stat = conn.createStatement();
    PreparedStatement prep1 = conn.prepareStatement(
            "insert into MedType values (?, ?);");
    PreparedStatement prep2 = conn.prepareStatement(
            "insert into Media values (?, ?,?, ?,?, ?);");

    System.out.print("Please choose a database(MedType or Media): ");
    String db=input.next();

    if(db.equalsIgnoreCase("MedType"))
    {
    System.out.print("Enter in ID: ");
    String answer1 = input.next();

    System.out.print("");
    String answer2 = input.nextLine();

    System.out.print("Enter in Description: ");
    String answer3 = input.nextLine();

    prep1.setString(1, answer1);//add values into cell
    prep1.setString(2, answer3);
    prep1.addBatch();//add the columns that have been entered

    }
conn.setAutoCommit(false);
    prep1.executeBatch();
    prep2.executeBatch();
    conn.setAutoCommit(true);




    System.out.print("Please Enter Query(One or All): ");
    String q=input.next();


    ResultSet rs= stat.executeQuery("select * from MedType;");


    if(q.equalsIgnoreCase("all")){
    while (rs.next()) {
        System.out.print("All ID = " + rs.getString("ID") + " ");
        System.out.println("All Description = " + rs.getString("Description"));}
    }
   if(q.equalsIgnoreCase("one")){
         System.out.print("Enter ID: ");

    }
   int idNum=input.nextInt();
    ResultSet oneRs = stat.executeQuery("select * from MedType Where"+ (rs.getString("ID")+"="+idNum));

   if(q.equalsIgnoreCase("one")){


        while (oneRs.next()) {
            System.out.print("ID = " + oneRs.getString("ID") + " ");
            System.out.println("Description = " + oneRs.getString("Description"));
        }
    }

    rs.close();
    oneRs.close();
    conn.close();

}
}

ResultSet oneRs = stat.executeQuery("select * from MedType Where"+
   (rs.getString("ID")+"="+idNum));

这就是我遇到麻烦的地方。创建一个语句,如果它的 id 等于用户输入,则从表中返回一些东西。我收到这个错误

Exception in thread "main" java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (near "=": syntax error)

4

2 回答 2

2

在查询中,您试图通过传递 id 来访问单行。在通常的 sql 查询中,我们通过传递一些信息来访问单行。select * from MedType where id=3此查询将返回您的结果集,其中包含 id 等于 3 的行或行。因此在您的代码中,您的查询应该是select * from MedType where id="+idNum+" if in your db id column是整数。并将此查询保留在 if 块中,即

 if(q.equalsIgnoreCase("one"))
{
         System.out.print("Enter ID: ");
         int idNum=input.nextInt();
    ResultSet oneRs = stat.executeQuery("select * from MedType Where id="+idNum+" ");
    // if id column in db is int if it is string then use id='"+idNum+"'                                            

        while (oneRs.next())
       {
            System.out.print("ID = " + oneRs.getString("ID") + " ");
            System.out.println("Description = " + oneRs.getString("Description"));
        }
 }
于 2013-05-03T09:59:21.487 回答
1

在您的查询中:

select * from MedType Where"+ (rs.getString("ID")+"="+idNum

您似乎试图从返回所有元组的第一个结果集中获取 ID。

这将不起作用,因为在 where 子句中 ID 将不存在(因为现在没有结果rs.next())。如果有结果,那么您可能会有类似 ' where 3 = 3' 的内容(3 将是先前返回值的结果。您是否尝试过简单地使用:

select * from MedType Where ID = " + idNum

希望这是有道理的。

于 2013-05-03T09:19:20.510 回答