jdbc_commit.java

来自「有关JDBC的使用一些编程实例,有关与数据库连接的代码」· Java 代码 · 共 98 行

JAVA
98
字号
/* * Copyright 1996 John Wiley & Sons, Inc. All Rights Reserved. Reproduction * or translation of this work beyond that permitted in Section 117 of the 1976 * United States Copyright Act without the express written permission of the * copyright owner is unlawful. Requests for further information should be  * addressed to Permissions Department, John Wiley & Sons, Inc. The  * purchaser may make back-up copies for his/her own use only and not for  * distribution or resale. The Publisher assumes no responsibility for errors,  * omissions, or damages, caused by the use of this  software or from the use * of the information contained herein. * */import java.net.URL;import java.sql.*;class jdbc_commit {  public static void main(String argv[]) {    try {      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); // JDBC-ODBC bridge      Class.forName("com.sybase.jdbc.SybDriver").newInstance();    // Sybase    } catch (Exception e) {      e.printStackTrace();    }    try {      // the url, userid, and password come in from argv, so error out if      // they weren't passed.      //      if (argv.length < 3) {         System.err.println("Usage:");         System.err.println("");         System.err.println("java jdbc_commit URL userid password");         System.err.println("");         System.err.println("  Examples:");         System.err.println(           "  java jdbc_commit jdbc:odbc:MSSQL javadb javadb");         System.err.println(           "  java jdbc_commit jdbc:sybase:Tds:localhost:4000 javadb javadb");         System.exit(0);      }      String url  = argv[0];      String user = argv[1];      String pwd  = argv[2];      // make a connection to the specified URL      //      Connection con = DriverManager.getConnection(url, user, pwd);      // set auto-commit mode off; it is on by default.       //      con.setAutoCommit(false);      // get a statement object      //      Statement stmt = con.createStatement();      String sql = "UPDATE food SET price = $78.98 " +                   "WHERE  product = 'Cheeseburger'";      stmt.executeUpdate(sql);      // issue the commit      //      con.commit();      // get a new connection, since the last one was closed      // (not all drivers are capable of disabling autoclose)      //      con = DriverManager.getConnection(url, user, pwd);      // get a new statement, since the last one was closed      //      stmt = con.createStatement();      // execute a query to check the value of price      //      ResultSet rs = stmt.executeQuery("SELECT price FROM food " +	                               "WHERE product = 'Cheeseburger'");      rs.next();      System.out.println( rs.getString(1) );         stmt.close();      con.close();    }    catch( Exception e ) {      System.out.println(e.getMessage());      e.printStackTrace();    }  }}  

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?