Advertisement
Guest User

Untitled

a guest
Jun 9th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class GetTweets {
  4.  
  5.  
  6.  
  7. public static void main (String[] args){
  8.  
  9. MysqlDB MyDB = new MysqlDB();
  10. Connection conn = MysqlDB.mysqlConnect();
  11. ResultSet brands = MysqlDB.getSearchNames(conn);
  12.  
  13. try{
  14. while(brands.next()){
  15. String brand_name = brands.getString("name");
  16. System.out.println(brand_name);
  17. }
  18. }
  19. catch (Exception e){
  20. System.out.println("Query Error " + e);
  21. }
  22.  
  23.  
  24. }
  25.  
  26.  
  27.  
  28. static class MysqlDB{
  29. public MysqlDB() {}
  30.  
  31. public static Connection mysqlConnect() {
  32. try{
  33. String userName = "bob";
  34. String password = "pass";
  35. String url = "jdbc:mysql://localhost/test";
  36. Class.forName ("com.mysql.jdbc.Driver").newInstance();
  37. Connection conn = DriverManager.getConnection (url,userName,password);
  38. System.out.println("Database Connection Established");
  39. return conn;
  40. }
  41. catch (Exception e){
  42. System.err.println ("Cannot connect to database server" + e);
  43. return null;
  44. }
  45. }
  46.  
  47. public static ResultSet getSearchNames(Connection conn){
  48. try {
  49. Statement stmt = conn.createStatement();
  50. ResultSet rs = stmt.executeQuery("SELECT name from brands");
  51. return rs;
  52. }
  53. catch (SQLException ex){
  54. // handle any errors
  55. System.out.println("SQLException: " + ex.getMessage());
  56. System.out.println("SQLState: " + ex.getSQLState());
  57. System.out.println("VendorError: " + ex.getErrorCode());
  58. return null;
  59. }
  60.  
  61. }
  62.  
  63. }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement