<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FDatabase_SQL_JDBC%2FSQL_Interpreter</id>
		<title>Java/Database SQL JDBC/SQL Interpreter - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FDatabase_SQL_JDBC%2FSQL_Interpreter"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Database_SQL_JDBC/SQL_Interpreter&amp;action=history"/>
		<updated>2026-04-22T11:11:01Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/Database_SQL_JDBC/SQL_Interpreter&amp;diff=6951&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Database_SQL_JDBC/SQL_Interpreter&amp;diff=6951&amp;oldid=prev"/>
				<updated>2010-06-01T06:34:35Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 06:34, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/Database_SQL_JDBC/SQL_Interpreter&amp;diff=6950&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Database_SQL_JDBC/SQL_Interpreter&amp;diff=6950&amp;oldid=prev"/>
				<updated>2010-05-31T18:01:44Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== A general-purpose SQL interpreter program ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2000 David Flanagan.  All rights reserved.&lt;br /&gt;
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.&lt;br /&gt;
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.&lt;br /&gt;
 * You may study, use, and modify it for any non-commercial purpose.&lt;br /&gt;
 * You may distribute it non-commercially as long as you retain this notice.&lt;br /&gt;
 * For a commercial use license, or to purchase the book (recommended),&lt;br /&gt;
 * visit http://www.davidflanagan.ru/javaexamples2.&lt;br /&gt;
 */&lt;br /&gt;
import java.io.BufferedReader;&lt;br /&gt;
import java.io.InputStreamReader;&lt;br /&gt;
import java.io.OutputStream;&lt;br /&gt;
import java.io.PrintWriter;&lt;br /&gt;
import java.sql.Connection;&lt;br /&gt;
import java.sql.DriverManager;&lt;br /&gt;
import java.sql.ResultSet;&lt;br /&gt;
import java.sql.ResultSetMetaData;&lt;br /&gt;
import java.sql.SQLException;&lt;br /&gt;
import java.sql.SQLWarning;&lt;br /&gt;
import java.sql.Statement;&lt;br /&gt;
/**&lt;br /&gt;
 * A general-purpose SQL interpreter program.&lt;br /&gt;
 */&lt;br /&gt;
public class ExecuteSQL {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    Connection conn = null; // Our JDBC connection to the database server&lt;br /&gt;
    try {&lt;br /&gt;
      String driver = null, url = null, user = &amp;quot;&amp;quot;, password = &amp;quot;&amp;quot;;&lt;br /&gt;
      // Parse all the command-line arguments&lt;br /&gt;
      for (int n = 0; n &amp;lt; args.length; n++) {&lt;br /&gt;
        if (args[n].equals(&amp;quot;-d&amp;quot;))&lt;br /&gt;
          driver = args[++n];&lt;br /&gt;
        else if (args[n].equals(&amp;quot;-u&amp;quot;))&lt;br /&gt;
          user = args[++n];&lt;br /&gt;
        else if (args[n].equals(&amp;quot;-p&amp;quot;))&lt;br /&gt;
          password = args[++n];&lt;br /&gt;
        else if (url == null)&lt;br /&gt;
          url = args[n];&lt;br /&gt;
        else&lt;br /&gt;
          throw new IllegalArgumentException(&amp;quot;Unknown argument.&amp;quot;);&lt;br /&gt;
      }&lt;br /&gt;
      // The only required argument is the database URL.&lt;br /&gt;
      if (url == null)&lt;br /&gt;
        throw new IllegalArgumentException(&amp;quot;No database specified&amp;quot;);&lt;br /&gt;
      // If the user specified the classname for the DB driver, load&lt;br /&gt;
      // that class dynamically. This gives the driver the opportunity&lt;br /&gt;
      // to register itself with the DriverManager.&lt;br /&gt;
      if (driver != null)&lt;br /&gt;
        Class.forName(driver);&lt;br /&gt;
      // Now open a connection the specified database, using the&lt;br /&gt;
      // user-specified username and password, if any. The driver&lt;br /&gt;
      // manager will try all of the DB drivers it knows about to try to&lt;br /&gt;
      // parse the URL and connect to the DB server.&lt;br /&gt;
      conn = DriverManager.getConnection(url, user, password);&lt;br /&gt;
      // Now create the statement object we&amp;quot;ll use to talk to the DB&lt;br /&gt;
      Statement s = conn.createStatement();&lt;br /&gt;
      // Get a stream to read from the console&lt;br /&gt;
      BufferedReader in = new BufferedReader(new InputStreamReader(&lt;br /&gt;
          System.in));&lt;br /&gt;
      // Loop forever, reading the user&amp;quot;s queries and executing them&lt;br /&gt;
      while (true) {&lt;br /&gt;
        System.out.print(&amp;quot;sql&amp;gt; &amp;quot;); // prompt the user&lt;br /&gt;
        System.out.flush(); // make the prompt appear now.&lt;br /&gt;
        String sql = in.readLine(); // get a line of input from user&lt;br /&gt;
        // Quit when the user types &amp;quot;quit&amp;quot;.&lt;br /&gt;
        if ((sql == null) || sql.equals(&amp;quot;quit&amp;quot;))&lt;br /&gt;
          break;&lt;br /&gt;
        // Ignore blank lines&lt;br /&gt;
        if (sql.length() == 0)&lt;br /&gt;
          continue;&lt;br /&gt;
        // Now, execute the user&amp;quot;s line of SQL and display results.&lt;br /&gt;
        try {&lt;br /&gt;
          // We don&amp;quot;t know if this is a query or some kind of&lt;br /&gt;
          // update, so we use execute() instead of executeQuery()&lt;br /&gt;
          // or executeUpdate() If the return value is true, it was&lt;br /&gt;
          // a query, else an update.&lt;br /&gt;
          boolean status = s.execute(sql);&lt;br /&gt;
          // Some complex SQL queries can return more than one set&lt;br /&gt;
          // of results, so loop until there are no more results&lt;br /&gt;
          do {&lt;br /&gt;
            if (status) { // it was a query and returns a ResultSet&lt;br /&gt;
              ResultSet rs = s.getResultSet(); // Get results&lt;br /&gt;
              printResultsTable(rs, System.out); // Display them&lt;br /&gt;
            } else {&lt;br /&gt;
              // If the SQL command that was executed was some&lt;br /&gt;
              // kind of update rather than a query, then it&lt;br /&gt;
              // doesn&amp;quot;t return a ResultSet. Instead, we just&lt;br /&gt;
              // print the number of rows that were affected.&lt;br /&gt;
              int numUpdates = s.getUpdateCount();&lt;br /&gt;
              System.out.println(&amp;quot;Ok. &amp;quot; + numUpdates&lt;br /&gt;
                  + &amp;quot; rows affected.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            // Now go see if there are even more results, and&lt;br /&gt;
            // continue the results display loop if there are.&lt;br /&gt;
            status = s.getMoreResults();&lt;br /&gt;
          } while (status || s.getUpdateCount() != -1);&lt;br /&gt;
        }&lt;br /&gt;
        // If a SQLException is thrown, display an error message.&lt;br /&gt;
        // Note that SQLExceptions can have a general message and a&lt;br /&gt;
        // DB-specific message returned by getSQLState()&lt;br /&gt;
        catch (SQLException e) {&lt;br /&gt;
          System.err.println(&amp;quot;SQLException: &amp;quot; + e.getMessage() + &amp;quot;:&amp;quot;&lt;br /&gt;
              + e.getSQLState());&lt;br /&gt;
        }&lt;br /&gt;
        // Each time through this loop, check to see if there were any&lt;br /&gt;
        // warnings. Note that there can be a whole chain of warnings.&lt;br /&gt;
        finally { // print out any warnings that occurred&lt;br /&gt;
          SQLWarning w;&lt;br /&gt;
          for (w = conn.getWarnings(); w != null; w = w&lt;br /&gt;
              .getNextWarning())&lt;br /&gt;
            System.err.println(&amp;quot;WARNING: &amp;quot; + w.getMessage() + &amp;quot;:&amp;quot;&lt;br /&gt;
                + w.getSQLState());&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    // Handle exceptions that occur during argument parsing, database&lt;br /&gt;
    // connection setup, etc. For SQLExceptions, print the details.&lt;br /&gt;
    catch (Exception e) {&lt;br /&gt;
      System.err.println(e);&lt;br /&gt;
      if (e instanceof SQLException)&lt;br /&gt;
        System.err.println(&amp;quot;SQL State: &amp;quot;&lt;br /&gt;
            + ((SQLException) e).getSQLState());&lt;br /&gt;
      System.err.println(&amp;quot;Usage: java ExecuteSQL [-d &amp;lt;driver&amp;gt;] &amp;quot;&lt;br /&gt;
          + &amp;quot;[-u &amp;lt;user&amp;gt;] [-p &amp;lt;password&amp;gt;] &amp;lt;database URL&amp;gt;&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    // Be sure to always close the database connection when we exit,&lt;br /&gt;
    // whether we exit because the user types &amp;quot;quit&amp;quot; or because of an&lt;br /&gt;
    // exception thrown while setting things up. Closing this connection&lt;br /&gt;
    // also implicitly closes any open statements and result sets&lt;br /&gt;
    // associated with it.&lt;br /&gt;
    finally {&lt;br /&gt;
      try {&lt;br /&gt;
        conn.close();&lt;br /&gt;
      } catch (Exception e) {&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * This method attempts to output the contents of a ResultSet in a textual&lt;br /&gt;
   * table. It relies on the ResultSetMetaData class, but a fair bit of the&lt;br /&gt;
   * code is simple string manipulation.&lt;br /&gt;
   */&lt;br /&gt;
  static void printResultsTable(ResultSet rs, OutputStream output)&lt;br /&gt;
      throws SQLException {&lt;br /&gt;
    // Set up the output stream&lt;br /&gt;
    PrintWriter out = new PrintWriter(output);&lt;br /&gt;
    // Get some &amp;quot;meta data&amp;quot; (column names, etc.) about the results&lt;br /&gt;
    ResultSetMetaData metadata = rs.getMetaData();&lt;br /&gt;
    // Variables to hold important data about the table to be displayed&lt;br /&gt;
    int numcols = metadata.getColumnCount(); // how many columns&lt;br /&gt;
    String[] labels = new String[numcols]; // the column labels&lt;br /&gt;
    int[] colwidths = new int[numcols]; // the width of each&lt;br /&gt;
    int[] colpos = new int[numcols]; // start position of each&lt;br /&gt;
    int linewidth; // total width of table&lt;br /&gt;
    // Figure out how wide the columns are, where each one begins,&lt;br /&gt;
    // how wide each row of the table will be, etc.&lt;br /&gt;
    linewidth = 1; // for the initial &amp;quot;|&amp;quot;.&lt;br /&gt;
    for (int i = 0; i &amp;lt; numcols; i++) { // for each column&lt;br /&gt;
      colpos[i] = linewidth; // save its position&lt;br /&gt;
      labels[i] = metadata.getColumnLabel(i + 1); // get its label&lt;br /&gt;
      // Get the column width. If the db doesn&amp;quot;t report one, guess&lt;br /&gt;
      // 30 characters. Then check the length of the label, and use&lt;br /&gt;
      // it if it is larger than the column width&lt;br /&gt;
      int size = metadata.getColumnDisplaySize(i + 1);&lt;br /&gt;
      if (size == -1)&lt;br /&gt;
        size = 30; // Some drivers return -1...&lt;br /&gt;
      if (size &amp;gt; 500)&lt;br /&gt;
        size = 30; // Don&amp;quot;t allow unreasonable sizes&lt;br /&gt;
      int labelsize = labels[i].length();&lt;br /&gt;
      if (labelsize &amp;gt; size)&lt;br /&gt;
        size = labelsize;&lt;br /&gt;
      colwidths[i] = size + 1; // save the column the size&lt;br /&gt;
      linewidth += colwidths[i] + 2; // increment total size&lt;br /&gt;
    }&lt;br /&gt;
    // Create a horizontal divider line we use in the table.&lt;br /&gt;
    // Also create a blank line that is the initial value of each&lt;br /&gt;
    // line of the table&lt;br /&gt;
    StringBuffer divider = new StringBuffer(linewidth);&lt;br /&gt;
    StringBuffer blankline = new StringBuffer(linewidth);&lt;br /&gt;
    for (int i = 0; i &amp;lt; linewidth; i++) {&lt;br /&gt;
      divider.insert(i, &amp;quot;-&amp;quot;);&lt;br /&gt;
      blankline.insert(i, &amp;quot; &amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    // Put special marks in the divider line at the column positions&lt;br /&gt;
    for (int i = 0; i &amp;lt; numcols; i++)&lt;br /&gt;
      divider.setCharAt(colpos[i] - 1, &amp;quot;+&amp;quot;);&lt;br /&gt;
    divider.setCharAt(linewidth - 1, &amp;quot;+&amp;quot;);&lt;br /&gt;
    // Begin the table output with a divider line&lt;br /&gt;
    out.println(divider);&lt;br /&gt;
    // The next line of the table contains the column labels.&lt;br /&gt;
    // Begin with a blank line, and put the column names and column&lt;br /&gt;
    // divider characters &amp;quot;|&amp;quot; into it. overwrite() is defined below.&lt;br /&gt;
    StringBuffer line = new StringBuffer(blankline.toString());&lt;br /&gt;
    line.setCharAt(0, &amp;quot;|&amp;quot;);&lt;br /&gt;
    for (int i = 0; i &amp;lt; numcols; i++) {&lt;br /&gt;
      int pos = colpos[i] + 1 + (colwidths[i] - labels[i].length()) / 2;&lt;br /&gt;
      overwrite(line, pos, labels[i]);&lt;br /&gt;
      overwrite(line, colpos[i] + colwidths[i], &amp;quot; |&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    // Then output the line of column labels and another divider&lt;br /&gt;
    out.println(line);&lt;br /&gt;
    out.println(divider);&lt;br /&gt;
    // Now, output the table data. Loop through the ResultSet, using&lt;br /&gt;
    // the next() method to get the rows one at a time. Obtain the&lt;br /&gt;
    // value of each column with getObject(), and output it, much as&lt;br /&gt;
    // we did for the column labels above.&lt;br /&gt;
    while (rs.next()) {&lt;br /&gt;
      line = new StringBuffer(blankline.toString());&lt;br /&gt;
      line.setCharAt(0, &amp;quot;|&amp;quot;);&lt;br /&gt;
      for (int i = 0; i &amp;lt; numcols; i++) {&lt;br /&gt;
        Object value = rs.getObject(i + 1);&lt;br /&gt;
        if (value != null)&lt;br /&gt;
          overwrite(line, colpos[i] + 1, value.toString().trim());&lt;br /&gt;
        overwrite(line, colpos[i] + colwidths[i], &amp;quot; |&amp;quot;);&lt;br /&gt;
      }&lt;br /&gt;
      out.println(line);&lt;br /&gt;
    }&lt;br /&gt;
    // Finally, end the table with one last divider line.&lt;br /&gt;
    out.println(divider);&lt;br /&gt;
    out.flush();&lt;br /&gt;
  }&lt;br /&gt;
  /** This utility method is used when printing the table of results */&lt;br /&gt;
  static void overwrite(StringBuffer b, int pos, String s) {&lt;br /&gt;
    int slen = s.length(); // string length&lt;br /&gt;
    int blen = b.length(); // buffer length&lt;br /&gt;
    if (pos + slen &amp;gt; blen)&lt;br /&gt;
      slen = blen - pos; // does it fit?&lt;br /&gt;
    for (int i = 0; i &amp;lt; slen; i++)&lt;br /&gt;
      // copy string into buffer&lt;br /&gt;
      b.setCharAt(pos + i, s.charAt(i));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>