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...