很难说出这里要问什么。这个问题模棱两可,含糊不清,不完整,过于宽泛,或者是修辞性的,不能以现在的形式得到合理的回答。有关澄清此问题以便重新打开的帮助,请访问帮助中心。
8年前关门了。
我不太懂sql,但我对java很在行,我只想知道如何从sql数据库中检索变量:“easydirectory”。比如:
String test = con.getQuery(query1).get(username);
显然,这不工作,但我想一个代码片段,这样做。以下是我的全部代码:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
public class Directory {
public static void main(String args[]) throws IOException, SQLException {
Connection con = null;
try {
// Load the JDBC driver
String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
Class.forName(driverName);
// Create a connection to the database
String serverName = "www.freesql.org";
String mydatabase = "EasyDirectory";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url
String username = "*********";
String password = "*********";
con = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
// Could not find the database driver
} catch (SQLException e) {
// Could not connect to the database
}
final JFrame frame = new JFrame("Directory");
frame.setPreferredSize(new Dimension(300, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JProgressBar searchprogress = new JProgressBar();
final JPanel panel = new JPanel();
final JButton searchbutton = new JButton("Search");
final JTextField searchfield = new JTextField();
searchfield.setPreferredSize(new Dimension(200, 30));
searchprogress.setPreferredSize(new Dimension(280, 30));
searchbutton.setLocation(100, 100);
/* Start Buffered Reader */
final List<String> housetypes = new ArrayList<String>();
String line = "";
BufferedReader br = new BufferedReader(new FileReader("Index.txt"));
while (line != null) {
line = br.readLine();
housetypes.add(line);
String seperation = br.readLine();
}
/* Finish Buffered Reader */
/* Start Content Code */
final JButton done = new JButton("Done");
done.setVisible(false);
JLabel housetype_label = new JLabel();
JLabel housenumber_label = new JLabel();
JLabel housestreet_label = new JLabel();
JLabel housepostal_label = new JLabel();
JLabel houseplace_label = new JLabel();
/* Finish Content Code */
/* Start Button Code */
done.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
searchfield.setEnabled(true);
done.setVisible(false);
searchbutton.setVisible(true);
searchprogress.setValue(0);
}
});
searchbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
searchprogress.setValue(100);
String searchquery = searchfield.getText();
searchprogress.setValue(100);
searchfield.setEnabled(false);
done.setVisible(true);
searchbutton.setVisible(false);
for (String housetype : housetypes) {
if (searchquery.equals(housetype)) {
String housepath = housetype + "/" + housetype + ".txt";
System.out.println(housepath);
try {
BufferedReader housebr = new BufferedReader(new FileReader(housepath));
String housename_query = housebr.readLine();
String housenumber_query = housebr.readLine();
String housestreet_query = housebr.readLine();
String houselocality_query = housebr.readLine();
String housepostal_query = housebr.readLine();
System.out.println(housepostal_query);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
/* Finish Button Code */
/* Test Field */
/* End Test Field */
panel.add(searchfield);
panel.add(done);
panel.add(searchbutton);
panel.add(searchprogress);
frame.setResizable(false);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(false);
/* Start Login Window */
int passtimes = 3;
final JFrame login = new JFrame("Login");
JPanel login_panel = new JPanel();
JLabel userlabel = new JLabel("Username: ");
JLabel passlabel = new JLabel(" Password: ");
JButton loginuser = new JButton("Login");
JButton cancel = new JButton("Cancel");
final JTextField user_field = new JTextField();
user_field.setPreferredSize(new Dimension(100,30));
final JPasswordField pass_field = new JPasswordField();
pass_field.setPreferredSize(new Dimension(100,30));
ImageIcon icon = new ImageIcon("Images/Logo.png");
ImageIcon space = new ImageIcon("Images/Spacing.png");
JLabel logo = new JLabel();
JLabel spacing = new JLabel();
logo.setIcon(icon);
login.setPreferredSize(new Dimension(200,212));
login_panel.add(logo);
login_panel.add(userlabel);
login_panel.add(user_field);
login_panel.add(passlabel);
login_panel.add(pass_field);
login_panel.add(spacing);
login_panel.add(loginuser);
login_panel.add(cancel);
login.add(login_panel);
login.pack();
login.setVisible(true);
login.setLocationRelativeTo(null);
loginuser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String user_input = user_field.getText();
String pass_input = pass_field.getText();
String username = "Tom";
String password = "******";
if(user_input.equals(username)){
if(pass_input.equals(password)){
user_field.setEnabled(false);
pass_field.setEnabled(false);
frame.setVisible(true);
login.setVisible(false);
}
else{//If Password AND Username is incorrect
JOptionPane.showMessageDialog(panel, "Password and/or Username Is Incorrect.", "Failed Login", JOptionPane.ERROR_MESSAGE);
}
}
else{ //If Username is incorrect
JOptionPane.showMessageDialog(panel, "Password and/or Username Is Incorrect.", "Failed Login", JOptionPane.ERROR_MESSAGE);
}
}
});
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
}
}
谢谢,非常感谢您的帮助!
1条答案
按热度按时间m2xkgtsf1#
通过阅读连接的api(这里:http://docs.oracle.com/javase/6/docs/api/java/sql/connection.html)我想它应该是这样的:
在旁注上。我不喜欢把gui和数据库逻辑放在同一个类中。您真的应该通过应用mvc模式或类似的方法来分离关注点。例如,您可以为gui创建一个类,一个用于连接数据库的类,以及一个用于启动应用程序并将其他两个类绑定在一起的类。