JAVA MySQL database connectivity sample program
Below is the the code for Java MySQL database create connection and fetch the data from the database.
You need to download Jar or add dependency for java MySQL connector. Please refer following link for the same:
https://mvnrepository.com/artifact/mysql/mysql-connector-java
Below is the sample code:
package test_mysql_connection_demo;
import java.awt.BorderLayout;
import java.sql.*;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Test_mysql_connection_demo {
public static void main(String args[]){
Test_mysql tm = new Test_mysql();
tm.conData();
boolean b = tm.exSQL("select * from student");
System.out.println(b);
}
}
class Test_mysql {
public Connection con;
void conData() {
try {
String host = "jdbc:mysql://local or server ip/database_name";
String uName = "username";
String uPass= "password";
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(host, uName, uPass);
JOptionPane.showMessageDialog(null, "Connection Successful...");
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, e, "Connection Error", JOptionPane.ERROR_MESSAGE);
}
}
public ResultSet rsFetch(String query) {
ResultSet rs = null;
try {
conData();
rs = con.createStatement().executeQuery(query);
return rs;
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, e, "ResultSet Query Error", JOptionPane.ERROR_MESSAGE);
return rs;
}
}
public boolean exSQL(String query) {
boolean done;
try {
done = true;
conData();
con.createStatement().execute(query);
try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
System.out.println("The columns in the table are: ");
System.out.println("Table: " + rs.getMetaData().getTableName(1));
for (int i = 1; i<= rs.getMetaData().getColumnCount(); i++){
System.out.println("Column " +i + " "+ rs.getMetaData().getColumnName(i));
} System.out.println("==============================================================================================");
int i=1;
while (rs.next()) {
String studentid = rs.getString("studentid");
String studentName = rs.getString("studentName");
String Description = rs.getString("courseDescription");
String add1 = rs.getString("address");
String add2 = rs.getString("address2");
String city = rs.getString("city");
String state = rs.getString("state");
String zip = rs.getString("zip");
String phone = rs.getString("phone");
String Email = rs.getString("Email");
System.out.println("==============================================================================================");
System.out.println("studentid: " + studentid);
System.out.println("studentName: " + studentName);
System.out.println("courseDescription: " + courseDescription);
System.out.println("address: " + address);
System.out.println("address2: " + address2);
System.out.println("city: " + city);
System.out.println("state: " + state);
System.out.println("zip: " + zip);
System.out.println("phone: " + phone);
System.out.println("Email: " + Email);
System.out.println("==============================================================================================");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, e);
}
System.out.println("==============================================================================================");
con.close();
return done;
}
catch(Exception e) {
done = false;
JOptionPane.showMessageDialog(null, e, "Query Error", JOptionPane.ERROR_MESSAGE);
return done;
}
}
}
Comments
Post a Comment