Oi,
Estou com o seguinte erro:
Exception in thread “main” java.lang.ClassCastException: net.sourceforge.jtds.jdbc.ConnectionJDBC3
at Teste.inclusao(Teste.java:17)
at Main.main(Main.java:10)
Java Result: 1
CONSTRUÍDO COM SUCESSO (tempo total: 0 segundos)
Classe de conexão
import java.sql.*;
public class ConectaBd {
public Connection con = null;
public Statement stm;
private ResultSet resultado_RS;
private static final String URL = "jdbc:jtds:sqlserver://PC/BDteste";
private static final String DRIVER = "net.sourceforge.jtds.jdbc.Driver";
private static final String USUARIO = "sa";
private static final String SENHA = "123456";
public static Connection getConnection() throws SQLException{
try{
Class.forName(DRIVER);
System.out.println("Conectando a Banco de Dados...");
return DriverManager.getConnection(URL, USUARIO, SENHA);
}catch (ClassNotFoundException e) {
System.out.println("Failha ao caregar o drive");
throw new SQLException (e.getMessage());
}
}
public Connection desconectaBanco() {
try {
con.close();
}
catch(Exception e) {
e.printStackTrace();
}
return con;
}
}
Classe main:
import java.sql.SQLException;
public class Main {
public static void main(String[] args) throws SQLException {
Teste t = new Teste();
t.inclusao();
}
}
Classe Teste
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class Teste {
private String codigo, nome;
private Statement stm = null;
private ConectaBd conexao;
@SuppressWarnings("static-access")
public boolean inclusao() {
try {
ConectaBd con = new ConectaBd();
con.getConnection();
stm = (Statement) ConectaBd.getConnection();
PreparedStatement pstmt = stm.getConnection().prepareStatement(
"insert into tabTeste(codigo, nome) values (?, ?) ");
pstmt.setInt(1, 1);
pstmt.setString(2, "maria");
pstmt.executeUpdate();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Erro ao gravar", "Aviso do sistema", JOptionPane.INFORMATION_MESSAGE);
}
return true;
}
}