Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package excercise.b;
- import java.sql.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author triad
- */
- public class DBtest {
- String URL = "jdbc:mysql:localhost:3306/userdetails";
- String USER = "root";
- String PASS = "";
- //Table
- String table = "userdetails";
- String tablelogin = "logindetails";
- public Connection getConnection(){
- try {
- Connection c = DriverManager.getConnection(URL,USER,PASS);
- return c;
- } catch (SQLException ex) {
- Logger.getLogger(DBtest.class.getName()).log(Level.SEVERE, null, ex);
- return null;
- }
- }
- //Get Data from any table
- public ResultSet getTableData(String table){
- try {
- //To execute Query you require PreparedStatements and the Connection
- Connection c = getConnection();
- String sql = "SELECT * from " + table;
- PreparedStatement st = c.prepareStatement(sql);
- //When Selecting data use executeQuery, but if not getting data in a resultset like Update, delete, add use executeUpdate()
- ResultSet rs = st.executeQuery();
- return rs;
- } catch (SQLException ex) {
- Logger.getLogger(DBtest.class.getName()).log(Level.SEVERE, null, ex);
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement