Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. package bd;
  2.  
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7. import javax.swing.JTextField;
  8.  
  9. public class Model {
  10. ConexaoMVC con = new ConexaoMVC();
  11. //A CLASSE View CONTEM A PARTE GRAFICA DO PROGRAMA
  12. View visao = new View(null);
  13.  
  14. //INSERIR
  15. public void inserir (String jtfRa, String jtfNome){
  16. try{
  17. ResultSet rs;
  18. String sql;
  19.  
  20. con.conecta();
  21.  
  22.  
  23. sql="insert into Aluno values ('"+jtfRa+"','"+jtfNome+"')";
  24.  
  25. con.stm.executeUpdate(sql);
  26. System.out.println(sql);
  27. rs = con.stm.executeQuery("Select * from Aluno");
  28.  
  29. while(rs.next()){
  30. String ra = rs.getString("ra");
  31. String nome = rs.getString("nome");
  32.  
  33. con.stm.executeUpdate("commit");
  34.  
  35. }
  36. con.conexao.close();
  37. }
  38. catch(Exception e){
  39.  
  40. }
  41. }
  42. //ATUALIZAR
  43. public void atualizar(String jtfRa, String jtfNome){
  44. try{
  45. ResultSet rs;
  46. String sql;
  47.  
  48. //Connection con = DriverManager.getConnection(url);
  49. con.conecta();
  50.  
  51. sql="update aluno set ra='"+jtfRa+"', nome='"+jtfNome+"' where ra='"+jtfRa+"'";
  52. System.out.println("O SQL n"+sql);
  53.  
  54. con.stm.executeUpdate(sql);
  55. System.out.println(sql);
  56. rs = con.stm.executeQuery("Select * from Aluno");
  57. con.stm.executeUpdate("commit");
  58. /*while(rs.next()){
  59. String ra = rs.getString("ra");
  60. String nome = rs.getString("nome");
  61.  
  62. con.stm.executeUpdate("commit");
  63.  
  64. }*/
  65. con.conexao.close();
  66.  
  67.  
  68. }
  69. catch(SQLException SqlExc){
  70. System.out.println("Erro de SQL!"+SqlExc);
  71. }
  72. }
  73. //DELETAR
  74. public void deletar(String jtfRa){
  75. try{
  76. con.conecta();
  77.  
  78. String sql="delete from aluno where ra='"+jtfRa+"'";
  79. System.out.println("O SQL n"+sql);
  80. con.stm.executeUpdate(sql);
  81.  
  82. con.stm.executeUpdate("commit");
  83. con.conexao.close();
  84. }
  85. catch(SQLException SqlExc){
  86. System.out.println("Erro de SQL! n"+SqlExc);
  87. }
  88. }
  89. //PRIMEIRO
  90. public void primeiro(){
  91. try{
  92. con.conecta();
  93.  
  94. ResultSet rs;
  95. String RA,NOME;
  96.  
  97. rs = con.stm.executeQuery("Select * from Aluno");
  98. rs.first();
  99.  
  100. RA = rs.getString("ra");
  101. NOME = rs.getString("nome");
  102.  
  103. System.out.println(RA+"----"+NOME);
  104. visao.setRA(RA);
  105. visao.setNome(NOME);
  106.  
  107. //System.out.println(RA+"----"+NOME);
  108.  
  109.  
  110. } catch(SQLException SqlExc){ //trata os erros
  111.  
  112. System.out.println("Erro de SQL! n"+SqlExc);
  113.  
  114. }
  115. }
  116. //ANTERIOR
  117. public void anterior(){
  118. try{
  119. con.conecta();
  120.  
  121. ResultSet rs;
  122. String RA,NOME;
  123.  
  124. rs = con.stm.executeQuery("Select * from Aluno");
  125.  
  126.  
  127. rs.previous();
  128.  
  129. RA = rs.getString("ra");
  130. NOME = rs.getString("nome");
  131.  
  132. System.out.println(RA+"----"+NOME);
  133. visao.setRA(RA);
  134. visao.setNome(NOME);
  135.  
  136. //System.out.println(RA+"----"+NOME);
  137.  
  138.  
  139. } catch(SQLException SqlExc){ //trata os erros
  140.  
  141. System.out.println("Erro de SQL! n"+SqlExc);
  142.  
  143. }
  144. }
  145. //PROXIMO
  146. public void proximo(){
  147. try{
  148. con.conecta();
  149.  
  150. ResultSet rs;
  151. String RA,NOME;
  152.  
  153. rs = con.stm.executeQuery("Select ra,nome from Aluno");
  154.  
  155.  
  156. rs.next();
  157. RA = rs.getString("ra");
  158. NOME = rs.getString("nome");
  159.  
  160. System.out.println(RA+"----"+NOME);
  161. visao.setRA(RA);
  162. visao.setNome(NOME);
  163.  
  164.  
  165. //System.out.println(RA+"----"+NOME);
  166.  
  167.  
  168. } catch(SQLException SqlExc){ //trata os erros
  169.  
  170. System.out.println("Erro de SQL! n"+SqlExc);
  171.  
  172. }
  173. }
  174. //ULTIMO
  175. public void ultimo(){
  176. try{
  177. con.conecta();
  178.  
  179. ResultSet rs;
  180. String RA,NOME;
  181.  
  182. rs = con.stm.executeQuery("Select * from Aluno");
  183.  
  184.  
  185. rs.last();
  186.  
  187. RA = rs.getString("ra");
  188. NOME = rs.getString("nome");
  189.  
  190. System.out.println(RA+"----"+NOME);
  191. visao.setRA(RA);
  192. visao.setNome(NOME);
  193.  
  194. //System.out.println(RA+"----"+NOME);
  195.  
  196.  
  197. } catch(SQLException SqlExc){ //trata os erros
  198.  
  199. System.out.println("Erro de SQL! n"+SqlExc);
  200.  
  201. }
  202. }
  203.  
  204.  
  205.  
  206. }
  207.  
  208. package bd;
  209.  
  210. import java.sql.*;
  211. import javax.swing.*;
  212.  
  213. public class ConexaoMVC {
  214.  
  215. public Connection conexao;
  216. private String driver,url;
  217. Statement stm;
  218. public ConexaoMVC(){
  219. //driver="sun.jdbc.odbc.JdbcOdbcDriver";
  220. driver="oracle.jdbc.driver.OracleDriver";
  221. url="jdbc:oracle:thin:guilherme/1997@//localhost:1521/XE";
  222. }
  223.  
  224.  
  225.  
  226. public void conecta(){
  227. try{
  228.  
  229. // carrega o driver da ponte jdbc-odbc
  230. Class.forName(driver);
  231. // abre conexao com o banco de dados
  232. conexao=DriverManager.getConnection(url);
  233. System.out.println("Conexão executada com sucesso");
  234. stm = conexao.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
  235. //conexao.close();
  236. }
  237. catch(SQLException SqlExc){
  238. System.out.println("Erro de SQL!");
  239. }
  240.  
  241. catch(ClassNotFoundException exc){
  242. System.out.println("Classe não encontrada!");
  243. }
  244. }
  245.  
  246. public static void main(String args[]){
  247. Conexao ins=new Conexao();
  248. }
  249.  
  250.  
  251.  
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement