java - 连接按钮显示mysql数据库结果

标签 java mysql database button actionlistener

我想按下我在 GUI 中创建的一个名为“搜索”的按钮,该按钮显示数据库。我已成功将我的数据库类连接到服务器,现在我需要将我的 GUI 类连接到数据库,以便它可以调用服务器上的数据。

数据库的结果必须出现在我在下面发布的结果框架中。

我故意删除了 corecct URL 和端口号,因为我不想在此处显示它。但是,我已经检查过,我的本地存储的类可以使用正确的 URL、端口、用户和通行证等。

这是 GUI 类:

//Search Button------------------------------------------------------------------------------------ 
    JButton btnSearch = new JButton("Search");
    btnSearch.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date thedate = new Date();
            date = year + "-" + month + "-" + day;
            if (year == "YYYY" || month == "MM" || day == "DD") {
                String datestring = dateFormat.format(thedate); 
                date =  datestring;
            }
            System.out.println(date);
            System.out.println(eventType);
            System.out.println(timeofDay);
            System.out.println(priceofEnterance);
            System.out.println(ageNum);

            // Initializing the translator and sending the strings to it
            Translator translator = new Translator();
            String query;
            String[] types = new String[1];
            types[0] = eventType;
            query = translator.search_to_sql(date, types, timeofDay,
                    priceofEnterance, ageNum);
            System.out.println(query);

            // Initializing the database and invoking the search
            Database db = new Database("Database");
            ArrayList<Event> results = new ArrayList<Event>();
            results = db.search(query);

            // Printing the result of the search
            String resultString = "";
            for (Event ev : results) {
                resultString += ev.toString() + "\n" + "\n";
            }
            displayResults(resultString);
        }
    });
    btnSearch.setBounds(96, 184, 90, 28);
    SearchPanel.add(btnSearch);

    JLabel lblSearchArea = new JLabel("Search Area");
    lblSearchArea.setFont(new Font("Segoe Script", Font.BOLD | Font.ITALIC, 19));
    lblSearchArea.setEnabled(false);
    lblSearchArea.setBounds(55, 11, 157, 29);
    SearchPanel.add(lblSearchArea);


    // THE RESULTS PANEL STARTS
    // HERE--------------------------------------------------------------------------------------------------------------------------------

    JPanel ResultFrame = new JPanel();
    ResultFrame.setBackground(new Color(255, 255, 0));
    ResultFrame.setBounds(298, 94, 1064, 611);
    frame.getContentPane().add(ResultFrame);
    ResultFrame.setLayout(null);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(0, 0, 1090, 611);
    ResultFrame.add(scrollPane);
    textArea = new JTextArea("", 5, 10);
    textArea.setBackground(new Color(255, 255, 255));
    scrollPane.setViewportView(textArea);
    // textArea.setColumns(10);
    textArea.setEditable(false);

上面,我尝试调用数据库,但是当按下“SEARCH”时,它给了我这个错误:

java.sql.SQLException: resource Database.db not found: java.net.MalformedURLException: no     protocol: Database.db

这也是我的数据库类:

package EventSearcher;

import java.sql.*;

public class DBConnect {    
   public static void main(String args[]){
    String url = "jdbc:mysql://URLGOESHERE:/";

    String dbName = "test";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "name";
    String password = "pass";

    try{
        Class.forName(driver).newInstance();
        Connection conn = DriverManager.getConnection(url+dbName,userName,password);

        Statement stmt = conn.createStatement();
        String strsql = "SELECT * FROM e";

        ResultSet res = stmt.executeQuery(strsql);

        while(res.next()){
            System.out.println("E_ID"+res.getString(1));
            System.out.println("Name"+res.getString(2));
            System.out.println("Time"+res.getString(3));
            System.out.println("Organizer"+res.getString(4));
            System.out.println("Date"+res.getString(5));
            System.out.println("Description"+res.getString(6));
            System.out.println("Location"+res.getString(7));
            System.out.println("Price"+res.getString(8));
            System.out.println("Min_Age"+res.getString(9));

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

    }
}

最佳答案

您需要为 jdbc url 提供此格式

jdbc:mysql://localhost:3306/mysql

如您所见,您缺少端口(3306 - 这是 mysql 服务器的默认端口)。

关于java - 连接按钮显示mysql数据库结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27376192/

相关文章:

mysql - 查询3张表,永远持续

java - 以函数式风格将 Optional 转换为 boolean

java - GWT UiBinder NullPointerException

mysql - 使用自连接按两列分组时 SQL 查询速度较慢

database - 为什么使用 NoSQL 而不是物化 View ?

database - 如何使用 Erwin 执行数据沿袭

javax.smartcardio 读取所有智能卡

java - 用字符串替换文本

MySQL - 即使 InnoDB 打开时外键也不起作用 (Ubuntu 16.04)(错误代码 : 1215)

php - 停止两个jquery之间的冲突