我正在使用MySQLWorkbench和Eclipse。在MySQLWorkbench中我有一个数据库,我存储我的信息。现在我想点击我的UI中的一个按钮,并希望在Java中获取我的JTextArea中的信息。我尝试了很多,但注意到了作品。问题是我不知道如何在主类中访问actionListener。
当我执行我的代码时,我可以在控制台中看到它,但我想在TextArea中显示它。
那是我仍然拥有的代码:
public static void main (String[] args)
{
//String x = txtfldVorname.getText();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/dblabor","root","");
System.out.println("Connection successful");
String sql = "SELECT name,nachname FROM kunden";
ps= (PreparedStatement) con.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()){
String name = rs.getString("name");
String nachname = rs.getString("nachname");
System.out.print(" Nachname: " + name);
System.out.print(" Nachname: " + nachname);
}
rs.close();
/*System.out.println("Select kunden");
st = con.createStatement();
String sql = "SELECT * FROM kunden";
rs= st.executeQuery(sql);
String sql1 = "INSERT INTO kunden (name,nachname)"+ "VALUES(?,?)";
PreparedStatement ps = (PreparedStatement) con.prepareStatement(sql1);
ps.execute();
/*while(rs.next()) {
String vorname= rs.getString("name");
String nachname = rs.getString("nachname");
System.out.format("%s,%s",vorname,nachname);
}
*/
} catch (Exception e) {
System.err.println(e);
}
}
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonProf.addActionListener(al);
try {
String sql = "SELECT name,nachname FROM kunden";
ps= (PreparedStatement) con.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()){
String name = rs.getString("name");
String nachname = rs.getString("nachname");
textfeld.getText();
System.out.print("Name: " + name);
System.out.print(" Nachname: " + nachname);
}
}//try
catch (SQLException e1) {
e1.printStackTrace();
}//catch
}
..提前全部谢谢。