querydb.java

来自「beginJsp2.0外文书籍源代码」· Java 代码 · 共 40 行

JAVA
40
字号
package com.wrox.library;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;

public class Querydb {

  String title = "No title";
  float price=0.00f;

  public void init() {
    try {
      Context ctx = new InitialContext();
      if(ctx == null )
        throw new Exception("Oops - No Context");

      DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/BooksDB");

      if (ds != null) {
        Connection conn = ds.getConnection();

        if(conn != null)  {
          Statement stmt = conn.createStatement();
          ResultSet rst = stmt.executeQuery("select * from book");
          if(rst.next()) {
            title=rst.getString(2);
            price=rst.getFloat("Price");
          }

          conn.close();
        }
      }
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

  public String getTitle() { return title; }
  public float getPrice() { return price; }
}

⌨️ 快捷键说明

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