⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 courseappl.java

📁 java 北大青鸟 java 北大青鸟
💻 JAVA
字号:
/** (c)北大青鸟APTECH
 * All Rights Reserved
 */

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.PreparedStatement;

/** This class demonstrates the usage of PreparedStatement in SQL.
 * @version 1.0 26 August 2005
 * @author Ben
 */

class CourseAppl {
  /** represents connection with database. */
  private Connection con;
  /** represents the SQLServer path. */
  private String url;
  /** represents the machine name. */
  private String serverName;
  /** stores the port number. */
  private String portNumber;
  /** stores the database name. */
  private String databaseName;
  /** stores the user name. */
  private String userName;
  /** stores the password. */
  private String password;
  /** stores the sql query statement. */
  private String sql;

  /** Constructor. */

  CourseAppl() {
    url = "jdbc:microsoft:sqlserver://";
    serverName = "localhost";
    portNumber = "1433";
    databaseName = "test2";
    userName = "sa";
    password = "";
  }

  /** @return Url, Server name, Port number and Database name. */

  private String getConnectionUrl() {
    //constructing the connection string
    return url + serverName + ":" + portNumber + ";databaseName="
        + databaseName + ";";
  }

  /** @return the Connection status. */

  private java.sql.Connection getConnection() {
    try {
      Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
      //establishing the connection

      con = DriverManager.getConnection(
          getConnectionUrl(), userName, password);

      if (con != null) {
        System.out.println("Connection Successful!");      
      }
    }
    catch (Exception e) {
      e.printStackTrace();
      System.out.println("Error Trace in getConnection() : "
                         + e.getMessage());
    }
    return con;
  }

  /** Method. */

  public void display() {
    try {
      con = getConnection();
      /* */
      PreparedStatement pstmt = con.prepareStatement(
          "UPDATE Friends SET salary= ? WHERE name like ?");
          //需要注意的是下面setInt和setString中的第一个数字都是表示查询中的
          //第几个参数
      pstmt.setInt(1, 1000);
      pstmt.setString(2, "李四");
      pstmt.executeUpdate();
      System.out.println("记录更新!");
     
      Statement s = con.createStatement();
      sql = "SELECT * FROM friends";
      ResultSet rs = s.executeQuery(sql);
      while (rs.next()) {
        System.out.println(" ");
        System.out.print(rs.getString(1) + " ");     
        System.out.println(rs.getInt(4));
      }
   
      rs.close();
      pstmt.close();
      con.close();
  }catch (SQLException ce) {
      System.out.println(ce);
    }
  }

  /** This is a main method.
   * It demonstrates the usage of PreparedStatement in SQL
   * @param args passed to the main method
   */

  public static void main(String[] args) {
    CourseAppl retObj = new CourseAppl();
    retObj.display();
  }
}

⌨️ 快捷键说明

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