Guest User

Untitled

a guest
Aug 5th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. Access mysql data base from another system using java
  2. public void dbconnection() {
  3.  
  4. String name = "";
  5. String port = "3306";
  6. String user = "systech";
  7. String pass = "systech";
  8. String dbname = "cascade_demo";
  9. String host="192.168.1.61";
  10.  
  11. try {
  12.  
  13. Class.forName("com.mysql.jdbc.Driver");
  14.  
  15. String url = "jdbc:mysql://"+host+":"+ port + "/" + dbname;
  16. System.out.println("URL:" + url);
  17. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  18. Connection con = DriverManager.getConnection(url, user, pass);
  19. String qry2 = "select * from item_master";
  20. Statement st = con.createStatement();
  21. ResultSet rs = st.executeQuery(qry2);
  22. while (rs.next()) {
  23.  
  24. name = rs.getString(1);
  25. System.out.println("Name:" + name);
  26.  
  27. }
  28.  
  29.  
  30. rs.close();
  31. st.close();
  32. con.close();
  33.  
  34.  
  35. } catch (Exception e) {
  36. System.out.println("Exception:" + e);
  37. }
  38. }
  39.  
  40. public void dbconnection() {
  41.  
  42. String name = "";
  43. String port = "3306";
  44. String user = "systech";
  45. String pass = "systech";
  46. String dbname = "cascade_demo";
  47. String host="192.168.1.61";
  48.  
  49. try {
  50. String url = "jdbc:mysql://"+host+":"+ port + "/" + dbname;
  51. Class.forName("com.mysql.jdbc.Driver").newInstance ();
  52. Connection con = DriverManager.getConnection(url, user, pass);
  53. String qry2 = "select * from item_master";
  54. Statement st = con.createStatement();
  55. ResultSet rs = st.executeQuery(qry2);
  56. while (rs.next()) {
  57. System.out.println("Name:" + rs.getString(1));
  58. }
  59.  
  60. rs.close();
  61. st.close();
  62. con.close();
  63.  
  64.  
  65. } catch (Exception e) {
  66. System.out.println("Exception:" + e);
  67. }
  68. }
  69.  
  70. CommunicationsException: Communications link failure
  71.  
  72. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  73.  
  74. Class.forName("com.mysql.jdbc.Driver");
  75.  
  76. } finally {
  77. try {
  78. rs.close();
  79. st.close();
  80. con.close();
  81.  
  82. } catch( Exception e ) {
  83. e.printStackTrace();
  84. }
  85. }
Add Comment
Please, Sign In to add comment