Advanced Java mysql,oracle,... connect, full functions


/ Published in: Java
Save to your folder(s)

use This Class to conenct to any database mysql oracle access and postgresql and use functions to select update insert or update data from database
--


Copy this code and paste it in your HTML
  1. //package ur_pack;
  2. /**
  3. *
  4. * auth : Ila Idias
  5. * fb.com/idiasila
  6. *
  7. */
  8.  
  9. import java.sql.*;
  10.  
  11. public class OuvrConnection {
  12.  
  13. private String driver = "com.mysql.jdbc.Driver"; // pour Mysql (Wampserver ..etc)
  14.  
  15. /***************************************************************************/
  16.  
  17. // private String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; // pour access
  18. // private String driver = "org.postgresql.Driver"; // pour postgresql
  19. // private String driver = "oracle.jdbc.driver.OracleDriver"; // pour Oracle
  20.  
  21. /***************************************************************************/
  22.  
  23. private String url = "jdbc:mysql://localhost/gcn"; //Pour MySql ( gcn is the name of my database )
  24.  
  25. /***************************************************************************/
  26.  
  27. // private String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=myDB.mdb;"; //Pour access
  28. // private String url = "jdbc:postgresql://localhost/gcn"; //Pour postgresql
  29. // private String url = "jdbc:oracle:thin:@localhost:1521:gcn"; //Pour Oracle
  30.  
  31. /***************************************************************************/
  32.  
  33. private String dbUserId = "root";
  34. private String dbPassword = "";
  35.  
  36. // ******************************************************** For Select Requests ***************************
  37.  
  38. public ResultSet ReqSelect (String My_Sql)
  39. {
  40. try{
  41. Class.forName(driver).newInstance(); // instance my driver to connect
  42. Connection cn=DriverManager.getConnection(url,dbUserId,dbPassword); // get connect to my database
  43.  
  44. pr = cn.prepareStatement(My_Sql);
  45. rs = pr.executeQuery();
  46. return rs;
  47.  
  48. }catch(Exception e){
  49. e.printStackTrace();
  50. return null;
  51. }
  52. }
  53.  
  54. // ******************************************************** For Update & Insert into & Delete Requests ****
  55.  
  56. public String ReqUID (String My_Sql){
  57. try{
  58. Class.forName(driver).newInstance(); // instance my driver to connect
  59. Connection cn=DriverManager.getConnection(url,dbUserId,dbPassword); // get connect to my database
  60.  
  61. pr = cn.prepareStatement(My_Sql);
  62. int r;
  63. r = pr.executeUpdate(My_Sql);
  64.  
  65. return +r+ "_Done_";
  66.  
  67. }catch(Exception e){
  68. e.printStackTrace();
  69. return null;
  70. }
  71.  
  72. }
  73.  
  74. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.