从 actionPerformed 调用静态方法时出错,但如果从 static void main 调用则没有错误
error when calling a static method from actionPerformed but no error if called from static void main
这是一个尝试为数据库开发 CRUD 的程序的开始,我为连接创建了一个静态方法,还有另外 2 个我可以调用的静态方法,它们中的任何一个都来自 main,但是如果尝试从 actionPerformed 调用它们,它给我一个异常错误,提供了完整的代码。
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class DBgui extends WindowAdapter implements ActionListener{
Frame f_principal, f_vertabla, f_modificar, f_mostrartablas, f_creartabla, f_borrar;//contenedor
Button b_aceptar, b_cancelar;//componentes
Label label_nombre, label_atributo;
TextField tf_nombre, tf_atributo;
MenuBar mb; //barra para menus
Menu m_mostrar, m_crear, m_vertabla, m_modificar, m_borrar;
MenuItem mi_tablas, mi_tabla, mi_mostrardbs, mi_creardbs;
public DBgui() {
//MARCOS
f_principal=new Frame("Interfaz gráfica Base de datos");
f_creartabla=new Frame("Crear Tabla");
f_vertabla=new Frame("Ver Tabla");
f_modificar=new Frame("Modificar Registro");
f_borrar=new Frame("Borrar Registro");
//BOTONES
b_aceptar=new Button("Aceptar");
b_cancelar=new Button("Cancelar");
//ETIQUETAS
label_nombre=new Label("Nombre");
label_atributo=new Label("Atributos");
//CAMPO DE TEXTO
tf_nombre=new TextField();
tf_atributo=new TextField();
//MENU
mb=new MenuBar();
m_mostrar=new Menu("Mostrar");
m_crear=new Menu("Crear");
m_vertabla=new Menu("Ver Tabla");
m_modificar=new Menu("Modificar");
m_borrar=new Menu("Borrar");
mi_tablas=new MenuItem("Mostrar Tablas");
mi_tabla=new MenuItem("Crear Tabla");
mi_creardbs=new MenuItem("Crear Base de datos");
mi_mostrardbs=new MenuItem("Mostrar base de datos");
}
//Mostar el menu principal y construccion de orejas
public void mostrarMarco(){
f_principal.setSize(800,600);
//SE CONSTRUYE EL MENU
f_principal.setMenuBar(mb);
mb.add(m_mostrar);
mb.add(m_crear);
mb.add(m_vertabla);
mb.add(m_modificar);
mb.add(m_borrar);
m_mostrar.add(mi_mostrardbs);
m_mostrar.add(mi_tablas);
m_crear.add(mi_creardbs);
m_crear.add(mi_tabla);
f_principal.setVisible(true);
//OREJAS
mi_mostrardbs.addActionListener(this);
mi_tablas.addActionListener(this);
mi_tabla.addActionListener(this);
mi_creardbs.addActionListener(this);
m_vertabla.addActionListener(this);
m_modificar.addActionListener(this);
m_borrar.addActionListener(this);
}
//cuando se presiona un boton del menu se ejecuta
public void actionPerformed(ActionEvent ae){
if(ae.getActionCommand().equals("Mostrar base de datos")){
Connection db1;
db1=database("root","chino130284");
mostrarTablas(db1);//THIS WHERE THE PROBLEM IS, IF I CALL THE METHOD FROM HERE GIVES ME AN ERROR
}
}
public static void mostrarBaseDeDatos(Connection db) throws Exception{
Frame f_mostrarbasededatos;
f_mostrarbasededatos = new Frame("Mostar bases de datos");
f_mostrarbasededatos.setLayout(new FlowLayout());
f_mostrarbasededatos.setBounds(0,40,120,500);
f_mostrarbasededatos.setResizable(false);
Label etiquetamostarbasededatos;
Statement stmt=db.createStatement();
ResultSet sst_ResultSet = stmt.executeQuery("SHOW DATABASES");
while (sst_ResultSet.next()) {
System.out.println(sst_ResultSet.getString(1));
etiquetamostarbasededatos = new Label(sst_ResultSet.getString(1));
f_mostrarbasededatos.add(etiquetamostarbasededatos);
}
f_mostrarbasededatos.setVisible(true);
}
public static Connection database(String username, String password) {
Connection conn;
conn=null;
String url = "jdbc:mysql://localhost:3306/testdb";
//LOAD DRIVER
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e){
System.err.println("Could not load database driver!");
}
//CONNECT TO DATABASE
try {
conn = DriverManager.getConnection(url, username, password);
return conn;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void mostrarTablas(Connection db) throws Exception{
Statement stmt=db.createStatement();
String sqlshowtables = "SHOW TABLES";
ResultSet sst_ResultSet = stmt.executeQuery(sqlshowtables);
while (sst_ResultSet.next()) {
System.out.println(sst_ResultSet.getString(1));
}
}
public static void main(String[] args) throws Exception{
Connection db;
db=database("root","chino130284");
mostrarTablas(db);
mostrarBaseDeDatos(db);
DBgui gui=new DBgui();
gui.mostrarMarco();
db.close();
}
mostrarTablas
声明 java.lang.Exception
被抛出。用捕获 Exception
.
的 try
块环绕调用它
您可以从 main
调用它,因为它有:“throws Exception
”。
这是一个尝试为数据库开发 CRUD 的程序的开始,我为连接创建了一个静态方法,还有另外 2 个我可以调用的静态方法,它们中的任何一个都来自 main,但是如果尝试从 actionPerformed 调用它们,它给我一个异常错误,提供了完整的代码。
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class DBgui extends WindowAdapter implements ActionListener{
Frame f_principal, f_vertabla, f_modificar, f_mostrartablas, f_creartabla, f_borrar;//contenedor
Button b_aceptar, b_cancelar;//componentes
Label label_nombre, label_atributo;
TextField tf_nombre, tf_atributo;
MenuBar mb; //barra para menus
Menu m_mostrar, m_crear, m_vertabla, m_modificar, m_borrar;
MenuItem mi_tablas, mi_tabla, mi_mostrardbs, mi_creardbs;
public DBgui() {
//MARCOS
f_principal=new Frame("Interfaz gráfica Base de datos");
f_creartabla=new Frame("Crear Tabla");
f_vertabla=new Frame("Ver Tabla");
f_modificar=new Frame("Modificar Registro");
f_borrar=new Frame("Borrar Registro");
//BOTONES
b_aceptar=new Button("Aceptar");
b_cancelar=new Button("Cancelar");
//ETIQUETAS
label_nombre=new Label("Nombre");
label_atributo=new Label("Atributos");
//CAMPO DE TEXTO
tf_nombre=new TextField();
tf_atributo=new TextField();
//MENU
mb=new MenuBar();
m_mostrar=new Menu("Mostrar");
m_crear=new Menu("Crear");
m_vertabla=new Menu("Ver Tabla");
m_modificar=new Menu("Modificar");
m_borrar=new Menu("Borrar");
mi_tablas=new MenuItem("Mostrar Tablas");
mi_tabla=new MenuItem("Crear Tabla");
mi_creardbs=new MenuItem("Crear Base de datos");
mi_mostrardbs=new MenuItem("Mostrar base de datos");
}
//Mostar el menu principal y construccion de orejas
public void mostrarMarco(){
f_principal.setSize(800,600);
//SE CONSTRUYE EL MENU
f_principal.setMenuBar(mb);
mb.add(m_mostrar);
mb.add(m_crear);
mb.add(m_vertabla);
mb.add(m_modificar);
mb.add(m_borrar);
m_mostrar.add(mi_mostrardbs);
m_mostrar.add(mi_tablas);
m_crear.add(mi_creardbs);
m_crear.add(mi_tabla);
f_principal.setVisible(true);
//OREJAS
mi_mostrardbs.addActionListener(this);
mi_tablas.addActionListener(this);
mi_tabla.addActionListener(this);
mi_creardbs.addActionListener(this);
m_vertabla.addActionListener(this);
m_modificar.addActionListener(this);
m_borrar.addActionListener(this);
}
//cuando se presiona un boton del menu se ejecuta
public void actionPerformed(ActionEvent ae){
if(ae.getActionCommand().equals("Mostrar base de datos")){
Connection db1;
db1=database("root","chino130284");
mostrarTablas(db1);//THIS WHERE THE PROBLEM IS, IF I CALL THE METHOD FROM HERE GIVES ME AN ERROR
}
}
public static void mostrarBaseDeDatos(Connection db) throws Exception{
Frame f_mostrarbasededatos;
f_mostrarbasededatos = new Frame("Mostar bases de datos");
f_mostrarbasededatos.setLayout(new FlowLayout());
f_mostrarbasededatos.setBounds(0,40,120,500);
f_mostrarbasededatos.setResizable(false);
Label etiquetamostarbasededatos;
Statement stmt=db.createStatement();
ResultSet sst_ResultSet = stmt.executeQuery("SHOW DATABASES");
while (sst_ResultSet.next()) {
System.out.println(sst_ResultSet.getString(1));
etiquetamostarbasededatos = new Label(sst_ResultSet.getString(1));
f_mostrarbasededatos.add(etiquetamostarbasededatos);
}
f_mostrarbasededatos.setVisible(true);
}
public static Connection database(String username, String password) {
Connection conn;
conn=null;
String url = "jdbc:mysql://localhost:3306/testdb";
//LOAD DRIVER
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e){
System.err.println("Could not load database driver!");
}
//CONNECT TO DATABASE
try {
conn = DriverManager.getConnection(url, username, password);
return conn;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void mostrarTablas(Connection db) throws Exception{
Statement stmt=db.createStatement();
String sqlshowtables = "SHOW TABLES";
ResultSet sst_ResultSet = stmt.executeQuery(sqlshowtables);
while (sst_ResultSet.next()) {
System.out.println(sst_ResultSet.getString(1));
}
}
public static void main(String[] args) throws Exception{
Connection db;
db=database("root","chino130284");
mostrarTablas(db);
mostrarBaseDeDatos(db);
DBgui gui=new DBgui();
gui.mostrarMarco();
db.close();
}
mostrarTablas
声明 java.lang.Exception
被抛出。用捕获 Exception
.
try
块环绕调用它
您可以从 main
调用它,因为它有:“throws Exception
”。