简体   繁体   English

接受来自 Java 中的外部文本文件的 SQL 查询

[英]Accepting SQL Queries from an external text file in Java

I was assigned to make a Java program that accepts SQL queries from a text file line by line, with line 1 being the driver name, line 2 is the URL, line 3 and 4 is the username and password respectively and line 5 is the query. I was assigned to make a Java program that accepts SQL queries from a text file line by line, with line 1 being the driver name, line 2 is the URL, line 3 and 4 is the username and password respectively and line 5 is the query . So for example my text file would have the following:因此,例如我的文本文件将具有以下内容:

org.apache.derby.jdbc.ClientDriver
jdbc:derby://localhost:1527/STUDENTDB
app
app
SELECT * FROM StudentDb WHERE STUDENT_NAME = ?
Jesse

And here is the code:这是代码:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import static java.lang.Boolean.parseBoolean;
import static java.lang.Integer.parseInt;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;

public class TestPreparedStatement {
      public static void main (String args[]) throws FileNotFoundException, IOException {
    try {
      BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\PCUSER\\info.txt"));
      String driver = br.readLine();
      Class.forName(driver);
      System.out.println("LOADED DRIVER  ---> " + driver);

      String url = br.readLine();
      Connection con = DriverManager.getConnection (url, br.readLine(), br.readLine());
      System.out.println("CONNECTED TO   ---> "+ url);

            String queryStr = br.readLine();
            PreparedStatement ps = con.prepareStatement(queryStr);
            String argu = br.readLine();
            ps.setString(1, argu);

            String queryStr2 = br.readLine();

            ResultSet rs = ps.executeQuery();
            System.out.println("EXECUTED QUERY ---> " + queryStr);
            System.out.println("\nPROCESSING RESULTS:\n");
            while (rs.next()) 
            {
                System.out.println("Name: " + rs.getString("STUDENT_NAME").trim());
                System.out.println("Student Number: " + rs.getString("STUDENT_NUMBER").trim()); 
                System.out.println("Course: " + rs.getString("COURSE").trim()); 
            }

        rs.close();
        ps.close();
        con.close();
    } 

    catch (SQLException e) {
        e.printStackTrace();
    } 

    catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
  }

}

The SELECT command I managed to make it work but I'm not sure how I can implement other query commands like INSERT INTO and DELETE. SELECT 命令我设法使它工作,但我不确定如何实现其他查询命令,如 INSERT INTO 和 DELETE。 Like for example if I wanted to insert or if I wanted to update or delete .例如,如果我想插入或者我想更新删除. Basically, I need help in being able to use other commands besides SELECT.基本上,我需要帮助才能使用除 SELECT 之外的其他命令。

You run executeUpdate instead of executeQuery , the rest is just the same.您运行executeUpdate而不是executeQuery , rest 是一样的。 Check this out for examples.看看这个例子。 You'd have to detect, if you're dealing with a select statement or otherwise, which is also straight forward: queryStr.trim().toUpperCase().startsWith("SELECT ")你必须检测,如果你正在处理 select 语句或其他,这也是直截了当的: queryStr.trim().toUpperCase().startsWith("SELECT ")

I'm paranoid about input, it could be nice like "SELECT a FROM B" or not so nice " seLect a from b", which is still valid sql but a simple startsWith won't do.我对输入很偏执,它可能很好,比如“从 B 中选择一个”或不太好“seLect a 从 b”,这仍然是有效的 sql 但简单的startsWith不会做。 trim() removes leading blanks, toUpperCase makes it uppercase;) trim() 删除前导空格,toUpperCase 使其大写;)

To detect the correct number of parameters (the "?" in the queries) I'd recommend, not to derive it from the query but from the rest of your input.要检测正确数量的参数(查询中的“?”),我建议不要从查询中导出它,而是从输入的 rest 中导出。 In the query there could be string values containing question marks as well (INSERT INTO TABLE (COMMENT, GRADE), VALUES("To be or not to be?", ?)) , that makes it a bit complicated.在查询中可能还有包含问号的字符串值(INSERT INTO TABLE (COMMENT, GRADE), VALUES("To be or not to be?", ?)) ,这使它有点复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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