Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- public class Test {
- public static void main(String[] args) {
- try {
- // 1. Register JDBC Driver
- Class.forName("com.mysql.jdbc.Driver");
- // 2. Create Connection Object
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost/demoDB","root", "");
- // 3. Create Statement object and Execute a Query
- Statement stmt = con.createStatement();
- // 4. Extract data from result set
- ResultSet rs = stmt.executeQuery("select * from users");
- while(rs.next()) {
- System.out.println(rs.getInt("id") + " " + rs.getString("name"));
- }
- // 5. Close the connection
- con.close();
- } catch(Exception e){
- // Handle errors here
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement