Guest User

Untitled

a guest
Sep 4th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. package conexoes;
  2.  
  3. /*
  4. * To change this template, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import javax.swing.JOptionPane;
  13.  
  14. /**
  15. *
  16. * @author Br
  17. */
  18. public class ConexaoFirebird {
  19.  
  20. private Connection con = null; //variavel para conexao
  21. private Statement statement;
  22. private ResultSet resultSet;
  23. //jdbc:firebirdsql:127.0.0.1/3050:java/
  24. private final String databaseURL = "jdbc:firebirdsql:Localhost:C:/BLVendasFirebird/firebird/sisvenda.fdb";
  25. private final String user = "SYSDBA";
  26. private final String password = "masterkey";
  27. private final String driverName = "org.localhost.firebirdsql.jdbc.FBDriver";
  28.  
  29. /**
  30. * Abre uma conexao com o banco
  31. */
  32. public void conectar() {
  33. try {
  34. Class.forName(driverName).newInstance();
  35. this.con = DriverManager.getConnection(databaseURL, user, password);
  36. System.out.println("Conexão obtida com sucesso.");
  37. } catch (SQLException ex) {
  38. System.out.println("SQLException: " + ex.getMessage());
  39. System.out.println("SQLState: " + ex.getSQLState());
  40. System.out.println("VendorError: " + ex.getErrorCode());
  41. } catch (Exception e) {
  42. System.out.println("Problemas ao tentar conectar com o banco de dados: " + e);
  43. }
  44.  
  45. }
  46.  
  47. public void executarSQL(String sql) {
  48. try {
  49. this.statement = con.createStatement(
  50. ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  51. this.resultSet = this.statement.executeQuery(sql);
  52.  
  53. // while (this.getResultSet().next()) {
  54. // System.out.println(this.getResultSet().getInt(1));
  55. // }
  56. } catch (SQLException sqlex) {
  57. sqlex.printStackTrace();
  58. }
  59. }
  60.  
  61. public void executarUpdateSQL(String sql) {
  62. try {
  63. this.statement = con.createStatement(
  64. ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  65. this.statement.executeUpdate(sql);
  66.  
  67. // while (this.getResultSet().next()) {
  68. // System.out.println(this.getResultSet().getInt(1));
  69. // }
  70. } catch (SQLException sqlex) {
  71. sqlex.printStackTrace();
  72. }
  73. }
  74.  
  75. /**
  76. * Executa insert SQL
  77. *
  78. * @param pSQL
  79. * @return boolean
  80. */
  81. public int insertSQL(String pSQL) {
  82. int idRetorno = 0;
  83. try {
  84. this.statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  85. this.resultSet = this.statement.executeQuery(pSQL);
  86.  
  87. while (this.getResultSet().next()) {
  88. idRetorno = this.getResultSet().getInt(1);
  89. }
  90.  
  91. } catch (SQLException sqlex) {
  92. sqlex.printStackTrace();
  93. }
  94. return idRetorno;
  95. }
  96.  
  97. /**
  98. * encerra a conexão corrente
  99. *
  100. * @return boolean
  101. */
  102. public boolean fecharConexao() {
  103. try {
  104. if ((this.getResultSet() != null) && (this.statement != null)) {
  105. this.getResultSet().close();
  106. this.statement.close();
  107. }
  108. this.getCon().close();
  109. return true;
  110. } catch (SQLException e) {
  111. JOptionPane.showMessageDialog(null, e.getMessage());
  112. }
  113. return false;
  114. }
  115.  
  116. /**
  117. * @return the statement
  118. */
  119. public Statement getStatement() {
  120. return statement;
  121. }
  122.  
  123. /**
  124. * @return the resultSet
  125. */
  126. public ResultSet getResultSet() {
  127. return resultSet;
  128. }
  129.  
  130. /**
  131. * @return the con
  132. */
  133. public Connection getCon() {
  134. return con;
  135. }
  136.  
  137. /**
  138. * @param con the con to set
  139. */
  140. public void setCon(Connection con) {
  141. this.con = con;
  142. }
  143.  
  144. /**
  145. * @param statement the statement to set
  146. */
  147. public void setStatement(Statement statement) {
  148. this.statement = statement;
  149. }
  150.  
  151. /**
  152. * @param resultSet the resultSet to set
  153. */
  154. public void setResultSet(ResultSet resultSet) {
  155. this.resultSet = resultSet;
  156. }
  157. }
  158.  
  159. package conexoes;
  160.  
  161. /*
  162. * To change this template, choose Tools | Templates
  163. * and open the template in the editor.
  164. */
  165. import java.sql.Connection;
  166. import java.sql.DriverManager;
  167. import java.sql.ResultSet;
  168. import java.sql.SQLException;
  169. import java.sql.Statement;
  170. import javax.swing.JOptionPane;
  171.  
  172. /**
  173. *
  174. * @author Br
  175. */
  176. public class ConexaoFirebird {
  177.  
  178. private Connection con = null; //variavel para conexao
  179. private Statement statement;
  180. private ResultSet resultSet;
  181. //jdbc:firebirdsql:127.0.0.1/3050:java/
  182. private final String databaseURL = "jdbc:firebirdsql:Localhost:C:/BLVendasFirebird/firebird/sisvenda.fdb";
  183. private final String user = "SYSDBA";
  184. private final String password = "masterkey";
  185. private final String driverName = "org.localhost.firebirdsql.jdbc.FBDriver";
  186.  
  187. /**
  188. * Abre uma conexao com o banco
  189. */
  190. public void conectar() {
  191. try {
  192. Class.forName(driverName).newInstance();
  193. this.con = DriverManager.getConnection(databaseURL, user, password);
  194. System.out.println("Conexão obtida com sucesso.");
  195. } catch (SQLException ex) {
  196. System.out.println("SQLException: " + ex.getMessage());
  197. System.out.println("SQLState: " + ex.getSQLState());
  198. System.out.println("VendorError: " + ex.getErrorCode());
  199. } catch (Exception e) {
  200. System.out.println("Problemas ao tentar conectar com o banco de dados: " + e);
  201. }
  202.  
  203. }
  204.  
  205. public void executarSQL(String sql) {
  206. try {
  207. this.statement = con.createStatement(
  208. ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  209. this.resultSet = this.statement.executeQuery(sql);
  210.  
  211. // while (this.getResultSet().next()) {
  212. // System.out.println(this.getResultSet().getInt(1));
  213. // }
  214. } catch (SQLException sqlex) {
  215. sqlex.printStackTrace();
  216. }
  217. }
  218.  
  219. public void executarUpdateSQL(String sql) {
  220. try {
  221. this.statement = con.createStatement(
  222. ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  223. this.statement.executeUpdate(sql);
  224.  
  225. // while (this.getResultSet().next()) {
  226. // System.out.println(this.getResultSet().getInt(1));
  227. // }
  228. } catch (SQLException sqlex) {
  229. sqlex.printStackTrace();
  230. }
  231. }
  232.  
  233. /**
  234. * Executa insert SQL
  235. *
  236. * @param pSQL
  237. * @return boolean
  238. */
  239. public int insertSQL(String pSQL) {
  240. int idRetorno = 0;
  241. try {
  242. this.statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  243. this.resultSet = this.statement.executeQuery(pSQL);
  244.  
  245. while (this.getResultSet().next()) {
  246. idRetorno = this.getResultSet().getInt(1);
  247. }
  248.  
  249. } catch (SQLException sqlex) {
  250. sqlex.printStackTrace();
  251. }
  252. return idRetorno;
  253. }
  254.  
  255. /**
  256. * encerra a conexão corrente
  257. *
  258. * @return boolean
  259. */
  260. public boolean fecharConexao() {
  261. try {
  262. if ((this.getResultSet() != null) && (this.statement != null)) {
  263. this.getResultSet().close();
  264. this.statement.close();
  265. }
  266. this.getCon().close();
  267. return true;
  268. } catch (SQLException e) {
  269. JOptionPane.showMessageDialog(null, e.getMessage());
  270. }
  271. return false;
  272. }
  273.  
  274. /**
  275. * @return the statement
  276. */
  277. public Statement getStatement() {
  278. return statement;
  279. }
  280.  
  281. /**
  282. * @return the resultSet
  283. */
  284. public ResultSet getResultSet() {
  285. return resultSet;
  286. }
  287.  
  288. /**
  289. * @return the con
  290. */
  291. public Connection getCon() {
  292. return con;
  293. }
  294.  
  295. /**
  296. * @param con the con to set
  297. */
  298. public void setCon(Connection con) {
  299. this.con = con;
  300. }
  301.  
  302. /**
  303. * @param statement the statement to set
  304. */
  305. public void setStatement(Statement statement) {
  306. this.statement = statement;
  307. }
  308.  
  309. /**
  310. * @param resultSet the resultSet to set
  311. */
  312. public void setResultSet(ResultSet resultSet) {
  313. this.resultSet = resultSet;
  314. }
  315. }
Add Comment
Please, Sign In to add comment