Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class GetTweets {
- public static void main (String[] args){
- MysqlDB MyDB = new MysqlDB();
- Connection conn = MysqlDB.mysqlConnect();
- ResultSet brands = MysqlDB.getSearchNames(conn);
- try{
- while(brands.next()){
- String brand_name = brands.getString("name");
- System.out.println(brand_name);
- }
- }
- catch (Exception e){
- System.out.println("Query Error " + e);
- }
- }
- static class MysqlDB{
- public MysqlDB() {}
- public static Connection mysqlConnect() {
- try{
- String userName = "bob";
- String password = "pass";
- String url = "jdbc:mysql://localhost/test";
- Class.forName ("com.mysql.jdbc.Driver").newInstance();
- Connection conn = DriverManager.getConnection (url,userName,password);
- System.out.println("Database Connection Established");
- return conn;
- }
- catch (Exception e){
- System.err.println ("Cannot connect to database server" + e);
- return null;
- }
- }
- public static ResultSet getSearchNames(Connection conn){
- try {
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT name from brands");
- return rs;
- }
- catch (SQLException ex){
- // handle any errors
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement