Jump to content

Duvida - NetBeans


watt

Recommended Posts

  • 2 weeks later...

Boas!

estou a tentar ligar ao meu projecto uma bd em access, e estou com um erro que n estou a perceber... o código que tenho é o seguinte.

Class bd está no file bd.java.

package gestor;

import java.sql.*;

public class bd {

   

  public static Connection getConnection() throws Exception {

    Driver d = (Driver)Class.forName

    ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

    Connection c = DriverManager.getConnection(

    "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/dados.mdb"

      );

    return c; 

    }

 

 

  public void teste(){

      System.out.println("teste");

  }

}

Aparente-mente aqui está td ok, não dá erros.

depois tenho o ficheiro princincipal e aqui dentro estou a mexer no private void Frame1InternalFrameActivated que dá erro, e o condigo q tenho é o seguinte.

static Connection Conn;

private void Frame1InternalFrameActivated(javax.swing.event.InternalFrameEvent evt) {                                             

// TODO add your handling code here:

       

      Conn = lig.getConnection(); <- Erro - D:\escola\POO\Gestor\src\Principal.java:221: unreported exception java.lang.Exception; must be caught or declared to be thrown

      ResultSet rs;

      Statement stmt;

      String sql;

       

      sql =  "select * from Artigos";

      stmt = Conn.createStatement();

      rs = stmt.executeQuery(sql);

       

        jLabel6.setText("");

    }                             

será que alguém me pode dar uma ajuda ?!

Link to comment
Share on other sites

(...)must be caught or declared to be thrown(...)

Você tem public static Connection getConnection() throws Exception {... por isso quando chama o método ou faz:

/* declared to be thrown */
private void Frame1InternalFrameActivated(javax.swing.event.InternalFrameEvent evt) throws Exception {
.
.
.

Ou então ponha o código dentro de um try-catch:

private void Frame1InternalFrameActivated(javax.swing.event.InternalFrameEvent evt){
/* must be caught */
try{
	Conn = lig.getConnection();
      
	String sql =  "select * from Artigos";
	Statement stmt = Conn.createStatement();
	ResultSet rs = stmt.executeQuery(sql);
        
	jLabel6.setText("");

}catch(Exception e){
	//Exception handling...
};
.
.
.

JDBC

Cumprimentos 🙂

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.