Connecting a Java Application With MySql DataBase
Hello Readers, In this arcticle i have shown how to connect a java application with MYSQL Database Table and fetch data from that table?
-Now right click on project
1)now click on configure build path,
2)click on Add External Jars,
3)Now find your downloaded mysql connector jar file and click on that and say apply and close.
-Now the Referenced Libraries Directory will be created, in that jar files are present that you added in last step.
-Now in your class main method or create your own local method perform the following steps.
1)Register the driver
2)Create the Connection
-That's all for creating connection
-Now you can perform execution of queries according to your requirement.
-Performing SQL SELECT Database Operation In Java Application.
file name : MyClass.java
Full Example
Class Name MyClass.java
Here database name is mysql@123 and clientdetail is my Table name with ClientName Column, and using SELECT DISTINCT Statement to get Unique ClientName from the table.
Hope It Will Help You.
Thanks
Hello Readers, In this arcticle i have shown how to connect a java application with MYSQL Database Table and fetch data from that table?
- First of all you need to download the platform independent mysql connector jar file.
- After downloading jar file,create a project in Eclipse or in your favorite IDE with main method.
1)now click on configure build path,
2)click on Add External Jars,
3)Now find your downloaded mysql connector jar file and click on that and say apply and close.
-Now the Referenced Libraries Directory will be created, in that jar files are present that you added in last step.
1)Register the driver
Class.forName("com.mysql.jdbc.Driver");
2)Create the Connection
String url,username,password;
url="jdbc:mysql://localhost:3306/database_name";
username="root";//if
it is a localhost
password="";//if
it localhost then keep it blank.
Connection conn=DriverManager.getConnection(url,username,password);
-That's all for creating connection
-Now you can perform execution of queries according to your requirement.
-Performing SQL SELECT Database Operation In Java Application.
file name : MyClass.java
public class MyClass{
public static void main(String args[]){
try {
String query="select DISTINCT(ClientName) from clientdetail";
PreparedStatement st=conn.prepareStatement(query);
ResultSet rs = st.executeQuery();
while(rs.next()){
String cname=rs.getString("ClientName");
//it depend on your requireme
System.out.println(cname);
}
rs.close();
st.close();
conn.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
Full Example
Class Name MyClass.java
import java.sql.*;
public class MyClass
{
public static void main(String args[])throws ClassNotFoundException
{
//Registering the driver
Class.forName("com.mysql.jdbc.Driver");
try {
String
url,username,password;
url="jdbc:mysql://localhost:3306/mysql@123";
//note:mysql@123 is a database name.
username="root";//if it is a localhost
password="";//if it localhost then keep it blank.
Connection conn=DriverManager.getConnection(url,username,password);
String
query="select DISTINCT(ClientName) from
clientdetail";
PreparedStatement
st=conn.prepareStatement(query);
ResultSet
rs =st.executeQuery();
while(rs.next())
{
String
cname=rs.getString("ClientName");
//it depend on your requirements
System.out.println(cname);
}
rs.close();
st.close();
conn.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Hope It Will Help You.
Thanks
No comments