Can someone please take me through how to make a connection to a MySQL database using Java in Netbeans.
I have downloaded the driver from MySQL, unzipped it to my location, and added the driver to the editor. After adding the driver, I can see it in the editor and my database along with the tables.
However, when I use Java code to make a connection, I get the following exception message:
Exception:com.mysql.jdbc.Driver
My code is below, it is very simple and basic. However, I would welcome guidance on how to make a proper class where I can call it from the main rather than being place in the main.
Expand|Select|Wrap|Line Numbers
- import java.sql.* // -- this is placed right at the top!
- Connection con = null;
- try
- {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank","root","triumph");
- if(!con.isClosed())
- System.out.println("Successfully Connected");
- }catch(Exception e){
- System.err.println("Exception:" + e.getMessage());
- }
- }