Guest User

Untitled

a guest
Nov 9th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. import com.mysql.jdbc.PreparedStatement;
  2. import java.sql.Connection;
  3. import java.sql.Statement;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.ResultSetMetaData;
  7. import java.sql.SQLException;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10.  
  11.  
  12.  
  13.  
  14. public class Coneccao {
  15. private static final String DRIVER="com.mysql.jdbc.Driver";
  16. private static final String URL="jdbc:mysql://localhost:3306/mydb";
  17. private static final String USER="root";
  18. private static final String PASS="";
  19. boolean ok;
  20.  
  21. public static Connection getConnection() {
  22. try {
  23. Class.forName(DRIVER);
  24. } catch (ClassNotFoundException ex) {
  25. Logger.getLogger(Coneccao.class.getName()).log(Level.SEVERE, null, ex);
  26. }
  27. try {
  28. DriverManager.getConnection(URL,USER,PASS);
  29. } catch (SQLException ex) {
  30. Logger.getLogger(Coneccao.class.getName()).log(Level.SEVERE, null, ex);
  31. }
  32. //just testing..
  33.  
  34. return null;
  35.  
  36. }
  37. public static void closeConnection(Connection con) {
  38. if (con!= null){
  39. try {
  40. con.close();
  41. } catch (SQLException ex) {
  42. Logger.getLogger(Coneccao.class.getName()).log(Level.SEVERE, null, ex);
  43. }
  44. }
  45. }
  46. public static void closeConnection(Connection con, PreparedStatement stmt ) {
  47. closeConnection(con);
  48. if(stmt!=null){
  49. try {
  50. stmt.close();
  51. } catch (SQLException ex) {
  52. Logger.getLogger(Coneccao.class.getName()).log(Level.SEVERE, null, ex);
  53. }
  54. }
  55. }
  56. public static void closeConnection(Connection con, PreparedStatement stmt, ResultSet rs) {
  57. closeConnection(con,stmt);
  58. if(rs!=null){
  59. try {
  60. rs.close();
  61. } catch (SQLException ex) {
  62. Logger.getLogger(Coneccao.class.getName()).log(Level.SEVERE, null, ex);
  63. }
  64. }
  65. }
  66. public static void main(String args[])
  67. {
  68. Coneccao c= new Coneccao();
  69. c.getConnection();
  70. }
  71. }
  72.  
  73. package BEAN;
  74.  
  75. /**
  76. *
  77. * @author Palmira
  78. */
  79. public class Funcionario {
  80. private String idFuncionario;
  81. private String nome;
  82. private String apelido;
  83. private int idade;
  84. private String departamento;
  85.  
  86. public String getIdFuncionario() {
  87. return idFuncionario;
  88. }
  89.  
  90. public void setIdFuncionario(String idFuncionario) {
  91. this.idFuncionario = idFuncionario;
  92. }
  93.  
  94. public String getNome() {
  95. return nome;
  96. }
  97.  
  98. public void setNome(String nome) {
  99. this.nome = nome;
  100. }
  101.  
  102. public String getApelido() {
  103. return apelido;
  104. }
  105.  
  106. public void setApelido(String apelido) {
  107. this.apelido = apelido;
  108. }
  109.  
  110. public int getIdade() {
  111. return idade;
  112. }
  113.  
  114. public void setIdade(int idade) {
  115. this.idade = idade;
  116. }
  117.  
  118. public String getDepartamento() {
  119. return departamento;
  120. }
  121.  
  122. public void setDepartamento(String departamento) {
  123. this.departamento = departamento;
  124. }
  125.  
  126. }
  127.  
  128. package DAO;
  129.  
  130. import BEAN.Funcionario;
  131. import Model.Coneccao;
  132. import com.mysql.jdbc.PreparedStatement;
  133. import java.sql.Connection;
  134. import java.sql.SQLException;
  135. import java.util.logging.Level;
  136. import java.util.logging.Logger;
  137. import javax.swing.JOptionPane;
  138.  
  139. /**
  140. *
  141. * @author Palmira
  142. */
  143. public class ManipulandoBD {
  144.  
  145.  
  146. public static void create(Funcionario f) {
  147.  
  148. Connection con = Coneccao.getConnection();
  149. PreparedStatement stmt = null;
  150. try {
  151.  
  152. stmt=(PreparedStatement)con.prepareStatement("INSERT INTO funcionario (idFuncionario,Nome,Apelido,Idade,Departamento)VALUES(?,?,?,?,?)");
  153. stmt.setString(1, f.getIdFuncionario());//a posicao das colunas como colocamos na linha acima e string.
  154. stmt.setString(2, f.getNome());
  155. stmt.setString(3, f.getApelido());
  156. stmt.setInt(4, f.getIdade());
  157. stmt.setString(5, f.getDepartamento());
  158.  
  159. stmt.executeUpdate();//executa o statement
  160. System.out.println("salvo com sucesso");
  161. } catch (SQLException ex) {
  162. System.out.println("nao salvou");
  163. Logger.getLogger(ManipulandoBD.class.getName()).log(Level.SEVERE, null, ex);
  164. }finally{
  165. Coneccao.closeConnection(con, stmt);
  166. }
  167. }
  168. public static void main(String[] args) {
  169. Funcionario f= new Funcionario();
  170. f.setIdFuncionario("1");
  171. f.setDepartamento("contabilidade");
  172. f.setApelido("Sa");
  173. f.setNome("Palmira");
  174. f.setIdade(20);
  175. create(f);
  176. }
  177.  
  178. }
  179.  
  180. run:
  181. Exception in thread "main" java.lang.NullPointerException
  182. at DAO.ManipulandoBD.create(ManipulandoBD.java:30)
  183. at DAO.ManipulandoBD.main(ManipulandoBD.java:53)
  184. Picked up _JAVA_OPTIONS: -Xms256M
  185. Java Result: 1
Add Comment
Please, Sign In to add comment