我想按下我在名为"搜索"的GUI中创建的按钮。显示数据库。我已成功将我的Database类连接到服务器,现在我需要将我的GUI类连接到数据库,以便它可以调用服务器上的数据。
数据库的结果必须出现在我在下面发布的结果框架中。
我故意删除了corecct网址和端口号,因为我不想在这里显示它。但是,我已经检查过,我的本地存储类使用正确的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();
}
}
}
答案 0 :(得分:0)
您需要为jdbc url提供此格式
JDBC:MySQL的://本地主机:3306 / MySQL的
你可以看到你缺少端口(3306 - 这是mysql服务器的默认端口)。