Blogs >> Technology >>
How we connect Java run time environment with MySql.
After a few days ago, My brother called me, and said, How we connect a Java enviourment with MySql Server then i decided to write the solution of this query on my blog. So, Read carefully every steps :
If you connect java application with your MySql Server the first requirement is, java run time enviourmant.
For compilation of java program, you need a java copliler (Javac).You can get this complier from java software devlopment kit.
Now, Connecting to the MySql Server:
The following short program, connect.java shows that how we connect and disconnect from server running on the local host.So, here is the programm
import java.sql.*; public class Connect { public static void main (String[] args) { Connection conn = null; try { String userName = "testuser"; String password = "testpass"; String url = "jdbc:mysql://localhost/test"; Class.forName ("com.mysql.jdbc.Driver").newInstance (); conn = DriverManager.getConnection (url, userName, password); System.out.println ("Database connection established"); } catch (Exception e) { System.err.println ("Cannot connect to database server"); } finally { if (conn != null) { try { conn.close (); System.out.println ("Database connection terminated"); } catch (Exception e) { /* ignore close errors */ } } } } }
Now, after that compile connect.java to produce connect.class that contail a executaible code in it.
% javac Connect.java
then with class file you can connect and disconnect connection with MySql Server
% java Connect Database connection established Database connection terminated
If you found any problem in connection then check that you have a Java software devlopment kit
and make sure that MYSQL Connector/J driver is listed in your ClassPath Enviourmant Variable
|