Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void runQuery(Connection connection){
- try{
- Statement stmt=con.createStatement();
- ResultSet rs=stmt.executeQuery("select * from table");
- while(rs.next()) {
- System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
- }
- }catch(Exception e){
- //Handle exception
- }
- }
- public static Connection getDBConnection(){
- Connection con = null;
- try{
- Class.forName("com.mysql.jdbc.Driver");
- con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/db","user","password");
- }
- catch(Exception e){
- //Handle exception
- }
- return con;
- }
- public static void closeConnection(Connection connection){
- if(connection!=null && !connection.isClosed()){
- connection.close();
- }
- }
- public static void main(String[]args){
- Connection con = getDBConnection();
- runQuery(con);
- closeConnection(con);
- }
Add Comment
Please, Sign In to add comment