Results 1 to 3 of 3
  1. #1
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default How to connect a Java program with MySQL database?

    Hi all

    this thread is for beginners like me..just want to share the code which i tried out..

    import java.io.*;
    import java.sql.*;

    class Sample
    {
    public static void main(String ds[])
    {
    Connection con=null;
    Statement st=null;
    ResultSet rs=null;
    String FirstName="";
    try
    {

    Class.forName("com.mysql.jdbc.Driver").newInstance ();

    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdatabasename?","root","password?");
    st=con.createStatement() ;
    rs=st.executeQuery("SELECT fieldname? FROM yourTablename?") ;
    while(rs.next())
    {
    FirstName=rs.getString(1);
    System.out.println(fieldname);
    }

    }
    catch(Exception eer)
    {
    System.out.println(eer.toString());
    }
    }
    }

    if anything is wrong please correct me...

  2. #2
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    The "FirstName" in the code is my field name you can give yours..

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You need to close your resources in a finally block. At the moment you are not closing anything, and that is very bad.

Similar Threads

  1. Replies: 3
    Last Post: 09-18-2008, 03:07 PM
  2. MySQL Database and Java
    By shaggymac in forum Advanced Java
    Replies: 1
    Last Post: 05-01-2008, 09:01 PM
  3. Replies: 2
    Last Post: 04-17-2008, 05:02 PM
  4. Connect applet with mysql
    By baltimore in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 06:39 AM
  5. Connect applet with mysql
    By Felissa in forum Java Applets
    Replies: 3
    Last Post: 07-05-2007, 06:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •