You are on page 1of 4

1. Install SQLYog as the MySQL Gui so that you can view your database directly.

Use Key/registration code as 07a2da5a3322f539 and name as TaMaMBoLo. 2. Install Wamp Server for Mysql which will be stored in c:\wamp 3. Install mysql connector/j. and then after extracting it put it in java folder under program files then jdk folder then jre folder then bin folder then ext folder. Add the mysqlconnector jar file to the class path in JCreator after you click on Add Archive. You should see it in the class path you have just added it to. 4. Change the password in Mysql to NEWPASSWORD using the following procedure: a. Type cmd at Programs Run to get the command prompt b. Open the following folder using CD command to get to mysql folder C:\wamp\bin\mysql\mysql5.1.36\bin c. Then type Mysqladmin u root password NEW PASSWORD

d. Now try the following programme in JCreator and save it as JDBC.java to test the connection. If your connection is successful, you should get the output Successfully connected to MySQL server using TCP/IP...

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class JDBC { public static void main(String args[]) { Connection con = null; try { String url = "jdbc:mysql://localhost:3306/mysql"; Class.forName("com.mysql.jdbc.Driver").newInstance();

con = DriverManager.getConnection(url, "root", "NEWPASSWORD");

if(!con.isClosed()) System.out.println("Successfully connected to " + "MySQL server using TCP/IP..."); } catch(Exception e) { System.err.println("Exception: " + e.getMessage()); } finally { try { if(con != null) con.close(); } catch(SQLException e) {} } } }

Now write the following Java programme called CafeJolt that creates a table JoltData/ Ensure that you are able to see it from SQLYog after the programme has successfully compiled and run. import java.sql.*; //import all the JDBC classes

public class CafeJolt {

static String[] SQL = { "create table JoltData ("+ "programmer varchar (32),"+ "day varchar (3),"+ "cups integer);", "insert into JoltData values ('Gilbert', 'Mon', 1);",

"insert into JoltData values ('Wally', 'Mon', 2);", "insert into JoltData values ('Edgar', 'Tue', 8);", "insert into JoltData values ('Wally', 'Tue', 2);", "insert into JoltData values ('Eugene', 'Tue', 3);", "insert into JoltData values ('Josephine', 'Wed', 2);", "insert into JoltData values ('Eugene', 'Thu', 3);", "insert into JoltData values ('Gilbert', 'Thu', 1);", "insert into JoltData values ('Clarence', 'Fri', 9);", "insert into JoltData values ('Edgar', 'Fri', 3);", "insert into JoltData values ('Josephine', 'Fri', 4);", };

public static void main(String[] args) { String URL = "jdbc:mysql://localhost:3306/mysql"; String username = "root"; String password = "NEWPASSWORD";

try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (Exception e) { System.out.println("Failed to load JDBC/ODBC driver."); return; }

Statement stmt = null; Connection con=null; try {

con = DriverManager.getConnection (URL,username,password); stmt = con.createStatement(); } catch (Exception e) { System.err.println("problems connecting to "+URL); }

try { // execute SQL commands to create table, insert data for (int i=0; i<SQL.length; i++) { stmt.execute(SQL[i]); } con.close(); } catch (Exception e) { System.err.println("problems with SQL sent to "+URL+": "+e.getMessage()); }

} }

You might also like