import java.sql.*;
public class MySqlConnect {
public static void main (String[] args)
{
Connection connection = null;
try
{
String userName = "root";
String password = "bunty";
String url = "jdbc:mysql://localhost:3306/systdb";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
connection = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
}
catch (Exception e)
{
System.err.println ("Connection to database server cannot be establish");
}
finally
{
if (connection != null)
{
try
{
connection.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { System.out.println ("These connection errors can be ignored."); }
}
}
}
}
Collection and sharing of, interview questions and answers asked in various interviews, faqs and articles.....
How do you connect to a MySql Database using JDBC?
Here is a code snippet for that.The important thing here is to ensure that you have added MySql database driver classes(mysql.jar) in your classpath. Here goes the code snippet:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment