Connection of MySql database to Java Swing JFrame(Forms designed in Window Builder in eclipse)

Following is the code:

public static Connection getConnection() throws Exception{

try {

String driver="com.mysql.cj.jdbc.Driver";

String url="jdbc:mysql://127.0.0.1:3306/sch_db";

String username="root";

String password="Grace@908";

Class.forName(driver);

Connection conn=DriverManager.getConnection(url,username,password);

System.out.println("connected");

return conn;

} catch(Exception e) {System.out.println(e);}

return null;

}

getConnection() Function is written, driver is needed for connection that is mysql jdbc driver 

declared in String variable driver.(driver is downloaded from internet)

Variable url decalred name of mysql jdbc driver, mysql uses default IP address and Port when

installing mysql.sch_db is database name which is created in mysql.Remaining is Username(default)

and password(user defined).Connection is given alias name conn to be used in further coding,

define Driver Manager function with 3 arguments(url,username,password),when executed system

console displays connected message.Exception handling is there for non executable of code.  

Note: one can modify the above code,username,password and database schema are user specific.

Enjoy the CODING!!!...



Comments