Advertisement
Guest User

DBConnection

a guest
Oct 17th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package excercise.b;
  7.  
  8. import java.sql.*;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11.  
  12. /**
  13.  *
  14.  * @author triad
  15.  */
  16. public class DBtest {
  17.     String URL = "jdbc:mysql:localhost:3306/userdetails";
  18.     String USER = "root";
  19.     String PASS = "";
  20.     //Table
  21.     String table = "userdetails";
  22.     String tablelogin = "logindetails";
  23.    
  24.     public Connection getConnection(){
  25.         try {
  26.             Connection c = DriverManager.getConnection(URL,USER,PASS);
  27.             return c;
  28.         } catch (SQLException ex) {
  29.             Logger.getLogger(DBtest.class.getName()).log(Level.SEVERE, null, ex);
  30.             return null;
  31.         }
  32.     }
  33.    
  34.     //Get Data from any table
  35.     public ResultSet getTableData(String table){
  36.         try {
  37.             //To execute Query you require PreparedStatements and the Connection
  38.             Connection c = getConnection();
  39.             String sql = "SELECT * from " + table;
  40.            
  41.             PreparedStatement st = c.prepareStatement(sql);
  42.             //When Selecting data use executeQuery, but if not getting data in a resultset like Update, delete, add use executeUpdate()
  43.             ResultSet rs = st.executeQuery();
  44.             return rs;
  45.         } catch (SQLException ex) {
  46.             Logger.getLogger(DBtest.class.getName()).log(Level.SEVERE, null, ex);
  47.             return null;
  48.         }
  49.        
  50.     }
  51.    
  52.    
  53.    
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement