Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void runQuery(){ // This method does 3 things 1. Gets the db connection and 2. Runs the query 3. Closes the connection
- Connection con = null;
- try{
- Class.forName("com.mysql.jdbc.Driver");
- con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/db","user","password");
- Statement stmt=con.createStatement();
- ResultSet rs=stmt.executeQuery("select * from emp");
- while(rs.next()) {
- System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
- }
- }catch(Exception e){ //handle exception}
- finally{
- if(connection!=null)
- con.close();
- }
- }
Add Comment
Please, Sign In to add comment