It may be similar questions to other in SO but most answers were related to the server configuration or IDE Configuration. Here I am trying to achieve the connection to Local Mysql Database using Notepad as text editor for my Classes and CMD to run. So, here I have a Folder called 'Learning' where my register.java and register.class lies. My register.java has:
jbsubmit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/demo","root","LJV9txSRof44Xmvk");
String name = tfname.getText();
String email = tfemail.getText();
String pass = tpfpass.getText();
Statement stmt = con.createStatement();
stmt.executeQuery("INSERT INTO user (name, email, password) VALUES(name, email, pass)");
}
catch(Exception e){
e.printStackTrace();
}
}
});
I am aware that Class.forName("com.mysql.jdbc.Driver"); not required since v4 & my connector version is 5.1.42. When using Class.forName I get ClassNotFoundException: com.mysql.jdbc.Driver and when not using Class.forName I get No suitable driver found for jdbc:mysql://localhost:3306/dbname
I've tried
- Put the full path to the jarfile in the classpath (including the filename .jar)
- Checked classpath several times for confirmation
- using Class.forName("com.mysql.jdbc.Driver").newInstance();
- Keeping connector in C:\Program Files\Java\jdk1.8.0_121\bin as well as C:\Program Files\Java\jre1.8.0_121\lib\ext
- Restarted CMD everytime after classpath changes
- Checked mySql port and connection port above are same