Programming Tutorials

Using JDBC to connect to MySQL from Java Program

By: Rajan in JDBC Tutorials on 2006-12-11  

import java.sql.*;
import javax.sql.*;

public class jdbcdemo {

    public static void main(String args[]) {
        String dbtime;
        String dbUrl = "jdbc:mysql://your.database.domain/yourDBname";
        String dbClass = "com.mysql.jdbc.Driver";
        String query = "Select * FROM users";

        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(dbUrl);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            while (rs.next()) {
                dbtime = rs.getString(1);
                System.out.println(dbtime);
            } // end while

            con.close();
        } // end try

        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        catch (SQLException e) {
            e.printStackTrace();
        }

    } // end main

} // end class





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in JDBC )

Latest Articles (in JDBC)