Sunday, June 26, 2011

JDBC MySQL quick start !

public static void main(String[] args) throws SQLException
{
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
    }
    catch (ClassNotFoundException ex)
    {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    con = DriverManager.getConnection("jdbc:mysql://desktop:3306/employees", "sukanta", "sukanta");
    stmt = con.createStatement();
    ResultSet result = stmt.executeQuery("SELECT * FROM table1");
    while (result.next())
        System.out.println(result.getString(1) + " " + result.getString(2));
    result.close();
    con.close();
}

No comments:

Post a Comment