Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mysqlconn;
- import java.sql.*;
- import java.util.*;
- public class Mysqlconn {
- public static void main(String[] args)throws SQLException {
- try {
- String url="jdbc:mysql://localhost:3306/newdatabase";
- Properties prop=new Properties();
- prop.setProperty("user","root");
- prop.setProperty("password","");
- Driver d = new com.mysql.jdbc.Driver();
- Connection con = d.connect(url,prop);
- if(con == null) {
- System.out.println("connection failed");
- return;
- }
- String query = "SELECT *FROM user";
- Statement state = con.createStatement();
- ResultSet result = null;
- state.executeQuery(query);
- while(result.next()){
- // fetching result from database
- String fname = result.getString("first_name");
- String lname = result.getString("last_name");
- int age = result.getInt("age");
- System.out.println("First Name: " + fname + ", Last Name: " + lname + ", Age: " + age);
- }
- state.close();
- } catch (Exception e)
- {
- System.err.println("Got an exception!");
- System.err.println(e.getMessage());
- }
- }
- }
Add Comment
Please, Sign In to add comment