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

📄 accesshandler.java

📁 这是一个关于初学者怎样在java环境下学习简单数据库操作的例子
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -