package br;
import java.sql.*;
public class BDAcesso {
public static void registrar(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e) {
System.out.println("Classe do driver não encontrada:");
System.out.println(e.getMessage());
System.exit(0);
}
}
public static Connection conectar(){
Connection c = null;
try{
String url = "jdbc:odbc:Driver=" +
"{Microsoft Access Driver (*.mdb)};" +
"dbq=C:/Users/Antonio/Documents/Antônio/Ulbra 2012/Linguagem comercial/Faculdade.mdb";
c = DriverManager.getConnection(url,"","");
}
catch(SQLException e) {
System.out.println(e.getMessage());
System.exit(0);
}
return c;
}
public static String Insere(String dad []){
registrar();
Connection C = conectar();
String s = "";
try {
Statement stmt = C.createStatement();
String sql = "Insert into Aluno(Codigo, Nome, Sexo) Values('"+dad[0]+"','"+dad[1]+"','"+dad[2]+"')";
int NRO= stmt.executeUpdate (sql);
s="Gravado com sucesso\n\n";
s+="Registro gravado: "+NRO;
stmt.close();
C.close();
}
catch (SQLException e){
System.out.println(e.getSQLState());
}
return s;
}
}
package br;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class FormAluno extends BDAcesso {
static String Codigo;
static String Nome;
static String Sexo;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
System.out.println("Codigo");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Codigo= br.readLine();
}
catch(IOException e){
}
try{
System.out.println("Nome");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Nome= br.readLine();
}
catch(IOException e){
}
try{
System.out.println("Sexo");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Sexo= br.readLine();
}
catch(IOException e){
}
}
}
Gravar no bd
7 Respostas
Por favor, siga essas dicas:
Além disso, jamais faça:
catch (Exception e) {}
Pois isso retira da sua tela as mensagens de erro.
E nem:
catch (Exception e) {
System.out.println(e.getMessage());
}
Pois isso elimina a informação de StackTrace.
Use no lugar:
catch (Exception e) {
throw new RuntimeException(e);
}
Seria bom também que vc postasse que erro está dando.
não está dando erro, mas não grava na tabela do access que eu criei.
Você corrigiu os catchs, como eu falei?
ok corrigi e não aparece o erro, apenas não grava no bd access.
se o seu retorno “Gravado com sucesso” é retornado, significa que está entrando no tray… caso contrário seria disparado no catch, você está tendo esse retorno?
Não retorna nada
não retorna erro, tive uma dica que o erro está na linha 18, mas não consigo corrigir.