Advertisement
Guest User

Untitled

a guest
Oct 4th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. import java.sql.DriverManager;
  2. import java.sql.ResultSet;
  3. import java.sql.SQLException;
  4. import java.sql.Connection;
  5. import java.sql.Statement;
  6.  
  7. public class MySql {
  8. Connection conn;//connection to database
  9.  
  10. public MySql(){
  11. // build a default connection to database
  12. String driver = "com.mysql.jdbc.Driver";
  13. String url = "jdbc:mysql://128.195.204.8:3306/mpl";
  14. String user = "eecs118";
  15. String password = "miniproject2";
  16. try {
  17. Class.forName(driver);
  18. //connect to database
  19. conn = DriverManager.getConnection(url, user, password);
  20. if(!conn.isClosed())
  21. System.out.println("Succeeded connecting to the Database!");
  22. } catch (SQLException e) {
  23. System.out.println("MySQL Error");
  24. e.printStackTrace();
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. }
  29.  
  30. public void Connect(String url, String user, String password){
  31. //build a connection to database according to parameters
  32. String driver = "com.mysql.jdbc.Driver";
  33. try {
  34. Class.forName(driver);
  35. conn = DriverManager.getConnection(url, user, password);
  36. if(!conn.isClosed())
  37. System.out.println("Succeeded connecting to the Database!");
  38. } catch (SQLException e) {
  39. System.out.println("MySQL Error");
  40. e.printStackTrace();
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. }
  44. }
  45.  
  46. public ResultSet SelectData(String sql){
  47. //Select data from database, and return the result set
  48. ResultSet result = null;
  49. try {
  50. Statement stmt = conn.createStatement();
  51. //sql = "select * from students;";
  52. System.out.println("SQL: "+sql);
  53. result = stmt.executeQuery(sql);
  54. } catch (SQLException e) {
  55. System.out.println("MySQL Select Error");
  56. e.printStackTrace();
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. return result;
  61. }
  62.  
  63. public int UpdateData(String sql){
  64. //update database including create, delete, update, return rows affected
  65. int result = -1;
  66. try {
  67. Statement stmt = conn.createStatement();
  68. //sql = "select * from students;";
  69. System.out.println("SQL: "+sql);
  70. result = stmt.executeUpdate(sql);
  71. } catch (SQLException e) {
  72. System.out.println("MySQL Update Error");
  73. e.printStackTrace();
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. }
  77. return result;
  78. }
  79.  
  80. public void Close() throws SQLException{
  81. //Close the connection to database
  82. conn.close();
  83. }
  84.  
  85. public static void main(String[] args) throws Exception {
  86. MySql test = new MySql();
  87. test.Close();
  88. String url = "jdbc:mysql://128.195.204.8:3306/mpl";
  89. String user = "eecs118";
  90. String password = "miniproject2";
  91. test.Connect(url, user, password);
  92. ResultSet result = test.SelectData("select * from students");
  93. while (result.next()) {
  94. System.out.println(result.getInt(1) + "\t"
  95. + result.getString(2)+"\t"
  96. + result.getString(3)+"\t"
  97. + result.getInt(4));//
  98. }
  99.  
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement