Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- public class MySQLDemo
- {
- public static void main(String[] args)
- {
- try
- {
- Connection conn = null;
- /* Assuming that your database is on localhost */
- String url = "jdbc:mysql://localhost:3306/";
- /* Assumign name of your database is 'newdb' */
- String dbName = "newdb";
- /* default driver name */
- String driver = "com.mysql.jdbc.Driver";
- /* Default username is 'root' */
- String userName = "root";
- /* Default password is blank */
- String password = "";
- Class.forName(driver).newInstance();
- conn = DriverManager
- .getConnection(url + dbName, userName, password);
- /* If something like this
- * (Connection is com.mysql.jdbc.JDBC4Connection@2fecdb1b)
- * is printed without exception, then connection is established*/
- System.out.println("Connection is " + conn);
- }
- catch (Exception e)
- {
- System.out.println(e);
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement