List of usage examples for java.sql SQLWarning getSQLState
public String getSQLState()
SQLException
object. From source file:Main.java
public static void main(String[] args) throws Exception { String dbURL = "jdbc:odbc:Companies"; try {/*ww w. ja va 2 s . co m*/ // Load the jdbc-odbc bridge driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Enable logging DriverManager.setLogWriter(new PrintWriter((System.err))); System.out.println("Getting Connection"); Connection conn = DriverManager.getConnection(dbURL, "user", "password"); SQLWarning warn = conn.getWarnings(); while (warn != null) { System.out.println("SQLState: " + warn.getSQLState()); System.out.println("Message: " + warn.getMessage()); System.out.println("Vendor: " + warn.getErrorCode()); System.out.println(""); warn = warn.getNextWarning(); } conn.close(); } catch (ClassNotFoundException e) { System.out.println("Can't load driver " + e); } catch (SQLException e) { System.out.println("Database access failed " + e); } }
From source file:Connect.java
public static void main(String[] av) { String dbURL = "jdbc:odbc:Companies"; try {//www . ja v a2s . c o m // Load the jdbc-odbc bridge driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Enable logging DriverManager.setLogWriter(new PrintWriter((System.err))); System.out.println("Getting Connection"); Connection conn = DriverManager.getConnection(dbURL, "ian", ""); // user, // passwd // If a SQLWarning object is available, print its // warning(s). There may be multiple warnings chained. SQLWarning warn = conn.getWarnings(); while (warn != null) { System.out.println("SQLState: " + warn.getSQLState()); System.out.println("Message: " + warn.getMessage()); System.out.println("Vendor: " + warn.getErrorCode()); System.out.println(""); warn = warn.getNextWarning(); } // Do something with the connection here... conn.close(); // All done with that DB connection } catch (ClassNotFoundException e) { System.out.println("Can't load driver " + e); } catch (SQLException e) { System.out.println("Database access failed " + e); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);//w ww . ja v a 2s. c o m String serverName = "127.0.0.1"; String portNumber = "1433"; String mydatabase = serverName + ":" + portNumber; String url = "jdbc:JSQLConnect://" + mydatabase; String username = "username"; String password = "password"; Connection connection = DriverManager.getConnection(url, username, password); try { SQLWarning warning = connection.getWarnings(); while (warning != null) { String message = warning.getMessage(); String sqlState = warning.getSQLState(); int errorCode = warning.getErrorCode(); warning = warning.getNextWarning(); } Statement stmt = connection.createStatement(); warning = stmt.getWarnings(); if (warning != null) { } ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table"); while (resultSet.next()) { warning = resultSet.getWarnings(); if (warning != null) { } } } catch (SQLException e) { } }
From source file:Main.java
public static void main(String[] args) throws Exception { try {/*from w w w.j a va2s . com*/ Connection conn = getConnection(); // get a java.sql.Connection object SQLWarning warning = conn.getWarnings(); while (warning != null) { // process connection warning String message = warning.getMessage(); String sqlState = warning.getSQLState(); int errorCode = warning.getErrorCode(); warning = warning.getNextWarning(); } } catch (SQLException e) { // ignore the exception } finally { // close JDBC resources: ResultSet, Statement, Connection } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);// w w w . jav a 2s.co m Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int, name VARCHAR(30) );"); String INSERT_RECORD = "insert into survey(id, name) values(?,?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setInt(1, 1); pstmt.setString(2, "name"); pstmt.executeUpdate(); // Get warnings on PreparedStatement object SQLWarning warning = pstmt.getWarnings(); while (warning != null) { // Process statement warnings... String message = warning.getMessage(); String sqlState = warning.getSQLState(); int errorCode = warning.getErrorCode(); warning = warning.getNextWarning(); } ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:ExecuteSQL.java
public static void main(String[] args) { Connection conn = null; // Our JDBC connection to the database server try {//from w w w.ja v a 2 s . co m String driver = null, url = null, user = "", password = ""; // Parse all the command-line arguments for (int n = 0; n < args.length; n++) { if (args[n].equals("-d")) driver = args[++n]; else if (args[n].equals("-u")) user = args[++n]; else if (args[n].equals("-p")) password = args[++n]; else if (url == null) url = args[n]; else throw new IllegalArgumentException("Unknown argument."); } // The only required argument is the database URL. if (url == null) throw new IllegalArgumentException("No database specified"); // If the user specified the classname for the DB driver, load // that class dynamically. This gives the driver the opportunity // to register itself with the DriverManager. if (driver != null) Class.forName(driver); // Now open a connection the specified database, using the // user-specified username and password, if any. The driver // manager will try all of the DB drivers it knows about to try to // parse the URL and connect to the DB server. conn = DriverManager.getConnection(url, user, password); // Now create the statement object we'll use to talk to the DB Statement s = conn.createStatement(); // Get a stream to read from the console BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // Loop forever, reading the user's queries and executing them while (true) { System.out.print("sql> "); // prompt the user System.out.flush(); // make the prompt appear now. String sql = in.readLine(); // get a line of input from user // Quit when the user types "quit". if ((sql == null) || sql.equals("quit")) break; // Ignore blank lines if (sql.length() == 0) continue; // Now, execute the user's line of SQL and display results. try { // We don't know if this is a query or some kind of // update, so we use execute() instead of executeQuery() // or executeUpdate() If the return value is true, it was // a query, else an update. boolean status = s.execute(sql); // Some complex SQL queries can return more than one set // of results, so loop until there are no more results do { if (status) { // it was a query and returns a ResultSet ResultSet rs = s.getResultSet(); // Get results printResultsTable(rs, System.out); // Display them } else { // If the SQL command that was executed was some // kind of update rather than a query, then it // doesn't return a ResultSet. Instead, we just // print the number of rows that were affected. int numUpdates = s.getUpdateCount(); System.out.println("Ok. " + numUpdates + " rows affected."); } // Now go see if there are even more results, and // continue the results display loop if there are. status = s.getMoreResults(); } while (status || s.getUpdateCount() != -1); } // If a SQLException is thrown, display an error message. // Note that SQLExceptions can have a general message and a // DB-specific message returned by getSQLState() catch (SQLException e) { System.err.println("SQLException: " + e.getMessage() + ":" + e.getSQLState()); } // Each time through this loop, check to see if there were any // warnings. Note that there can be a whole chain of warnings. finally { // print out any warnings that occurred SQLWarning w; for (w = conn.getWarnings(); w != null; w = w.getNextWarning()) System.err.println("WARNING: " + w.getMessage() + ":" + w.getSQLState()); } } } // Handle exceptions that occur during argument parsing, database // connection setup, etc. For SQLExceptions, print the details. catch (Exception e) { System.err.println(e); if (e instanceof SQLException) System.err.println("SQL State: " + ((SQLException) e).getSQLState()); System.err.println( "Usage: java ExecuteSQL [-d <driver>] " + "[-u <user>] [-p <password>] <database URL>"); } // Be sure to always close the database connection when we exit, // whether we exit because the user types 'quit' or because of an // exception thrown while setting things up. Closing this connection // also implicitly closes any open statements and result sets // associated with it. finally { try { conn.close(); } catch (Exception e) { } } }
From source file:Main.java
static boolean checkForWarning(SQLWarning w) { if (w == null) { return false; }//www . j a va 2 s . c om do { System.err.println("Warning:\nMessage: " + w.getMessage()); System.err.println("SQL state: " + w.getSQLState()); System.err.println("Vendor code: " + w.getErrorCode() + "\n----------------"); } while ((w = w.getNextWarning()) != null); return true; }
From source file:JDBCQuery.java
private static void checkForWarning(SQLWarning warn) throws SQLException { // If a SQLWarning object was given, display the // warning messages. Note that there could be // multiple warnings chained together if (warn != null) { System.out.println("*** Warning ***\n"); while (warn != null) { System.out.println("SQLState: " + warn.getSQLState()); System.out.println("Message: " + warn.getMessage()); System.out.println("Vendor: " + warn.getErrorCode()); System.out.println(""); warn = warn.getNextWarning(); }//from w w w .j a v a2 s . c o m } }
From source file:com.oracle.tutorial.jdbc.JDBCTutorialUtilities.java
public static void printWarnings(SQLWarning warning) throws SQLException { if (warning != null) { System.out.println("\n---Warning---\n"); while (warning != null) { System.out.println("Message: " + warning.getMessage()); System.out.println("SQLState: " + warning.getSQLState()); System.out.print("Vendor error code: "); System.out.println(warning.getErrorCode()); System.out.println(""); warning = warning.getNextWarning(); }/* ww w. j ava2s . c o m*/ } }
From source file:com.xqdev.sql.MLSQL.java
private static void addWarnings(Element meta, SQLWarning w) { if (w == null) return;/*from ww w. java2 s . co m*/ Namespace sql = meta.getNamespace(); Element warnings = new Element("warnings", sql); meta.addContent(warnings); do { warnings.addContent(new Element("warning", sql).setAttribute("type", w.getClass().getName()) .addContent(new Element("reason", sql).setText(w.getMessage())) .addContent(new Element("sql-state", sql).setText(w.getSQLState())) .addContent(new Element("vendor-code", sql).setText("" + w.getErrorCode()))); w = w.getNextWarning(); } while (w != null); }