Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bd;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.swing.JTextField;
- public class Model {
- ConexaoMVC con = new ConexaoMVC();
- //A CLASSE View CONTEM A PARTE GRAFICA DO PROGRAMA
- View visao = new View(null);
- //INSERIR
- public void inserir (String jtfRa, String jtfNome){
- try{
- ResultSet rs;
- String sql;
- con.conecta();
- sql="insert into Aluno values ('"+jtfRa+"','"+jtfNome+"')";
- con.stm.executeUpdate(sql);
- System.out.println(sql);
- rs = con.stm.executeQuery("Select * from Aluno");
- while(rs.next()){
- String ra = rs.getString("ra");
- String nome = rs.getString("nome");
- con.stm.executeUpdate("commit");
- }
- con.conexao.close();
- }
- catch(Exception e){
- }
- }
- //ATUALIZAR
- public void atualizar(String jtfRa, String jtfNome){
- try{
- ResultSet rs;
- String sql;
- //Connection con = DriverManager.getConnection(url);
- con.conecta();
- sql="update aluno set ra='"+jtfRa+"', nome='"+jtfNome+"' where ra='"+jtfRa+"'";
- System.out.println("O SQL n"+sql);
- con.stm.executeUpdate(sql);
- System.out.println(sql);
- rs = con.stm.executeQuery("Select * from Aluno");
- con.stm.executeUpdate("commit");
- /*while(rs.next()){
- String ra = rs.getString("ra");
- String nome = rs.getString("nome");
- con.stm.executeUpdate("commit");
- }*/
- con.conexao.close();
- }
- catch(SQLException SqlExc){
- System.out.println("Erro de SQL!"+SqlExc);
- }
- }
- //DELETAR
- public void deletar(String jtfRa){
- try{
- con.conecta();
- String sql="delete from aluno where ra='"+jtfRa+"'";
- System.out.println("O SQL n"+sql);
- con.stm.executeUpdate(sql);
- con.stm.executeUpdate("commit");
- con.conexao.close();
- }
- catch(SQLException SqlExc){
- System.out.println("Erro de SQL! n"+SqlExc);
- }
- }
- //PRIMEIRO
- public void primeiro(){
- try{
- con.conecta();
- ResultSet rs;
- String RA,NOME;
- rs = con.stm.executeQuery("Select * from Aluno");
- rs.first();
- RA = rs.getString("ra");
- NOME = rs.getString("nome");
- System.out.println(RA+"----"+NOME);
- visao.setRA(RA);
- visao.setNome(NOME);
- //System.out.println(RA+"----"+NOME);
- } catch(SQLException SqlExc){ //trata os erros
- System.out.println("Erro de SQL! n"+SqlExc);
- }
- }
- //ANTERIOR
- public void anterior(){
- try{
- con.conecta();
- ResultSet rs;
- String RA,NOME;
- rs = con.stm.executeQuery("Select * from Aluno");
- rs.previous();
- RA = rs.getString("ra");
- NOME = rs.getString("nome");
- System.out.println(RA+"----"+NOME);
- visao.setRA(RA);
- visao.setNome(NOME);
- //System.out.println(RA+"----"+NOME);
- } catch(SQLException SqlExc){ //trata os erros
- System.out.println("Erro de SQL! n"+SqlExc);
- }
- }
- //PROXIMO
- public void proximo(){
- try{
- con.conecta();
- ResultSet rs;
- String RA,NOME;
- rs = con.stm.executeQuery("Select ra,nome from Aluno");
- rs.next();
- RA = rs.getString("ra");
- NOME = rs.getString("nome");
- System.out.println(RA+"----"+NOME);
- visao.setRA(RA);
- visao.setNome(NOME);
- //System.out.println(RA+"----"+NOME);
- } catch(SQLException SqlExc){ //trata os erros
- System.out.println("Erro de SQL! n"+SqlExc);
- }
- }
- //ULTIMO
- public void ultimo(){
- try{
- con.conecta();
- ResultSet rs;
- String RA,NOME;
- rs = con.stm.executeQuery("Select * from Aluno");
- rs.last();
- RA = rs.getString("ra");
- NOME = rs.getString("nome");
- System.out.println(RA+"----"+NOME);
- visao.setRA(RA);
- visao.setNome(NOME);
- //System.out.println(RA+"----"+NOME);
- } catch(SQLException SqlExc){ //trata os erros
- System.out.println("Erro de SQL! n"+SqlExc);
- }
- }
- }
- package bd;
- import java.sql.*;
- import javax.swing.*;
- public class ConexaoMVC {
- public Connection conexao;
- private String driver,url;
- Statement stm;
- public ConexaoMVC(){
- //driver="sun.jdbc.odbc.JdbcOdbcDriver";
- driver="oracle.jdbc.driver.OracleDriver";
- url="jdbc:oracle:thin:guilherme/1997@//localhost:1521/XE";
- }
- public void conecta(){
- try{
- // carrega o driver da ponte jdbc-odbc
- Class.forName(driver);
- // abre conexao com o banco de dados
- conexao=DriverManager.getConnection(url);
- System.out.println("Conexão executada com sucesso");
- stm = conexao.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
- //conexao.close();
- }
- catch(SQLException SqlExc){
- System.out.println("Erro de SQL!");
- }
- catch(ClassNotFoundException exc){
- System.out.println("Classe não encontrada!");
- }
- }
- public static void main(String args[]){
- Conexao ins=new Conexao();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement