accesshandler.java

来自「java21pro_source 21天学通java2全书源代码」· Java 代码 · 共 45 行

JAVA
45
字号
import java.sql.*;
import java.util.*;

public class AccessHandler {
    public Vector getRandomSite() {
        Connection conn = getMySqlConnection();
        Vector response = new Vector();
        try {
            int rand = (int)Math.floor(1000 * Math.random());
            Statement st = conn.createStatement();
            ResultSet rec = st.executeQuery(
                "SELECT * FROM sites WHERE (ID=" +
                    rand + ")");
            if (rec.next()) {
                response.addElement("ok");
                response.addElement(rec.getString("url"));
                response.addElement(rec.getString("title"));
                response.addElement(rec.getString("description"));
            } else {
                response.addElement("database error: no records found");
            }
        } catch (SQLException sqe) {
            response.addElement("database error: " + sqe.getMessage());
        }
        return response;
    }

    private Connection getMySqlConnection() {
        Connection conn = null;
        String data = "jdbc:odbc:DmozDatabase";
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection(
                data, "", "");
        } catch (SQLException s) {
            System.out.println("SQL Error: " + s.toString() + " "
                + s.getErrorCode() + " " + s.getSQLState());
        } catch (Exception e) {
            System.out.println("Error: " + e.toString()
                + e.getMessage());
        }
        return conn;
    }
}

⌨️ 快捷键说明

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