I am just a beginner in java and working on core java....not on any tool such as Netbeans or Eclipse. It is simply notepad based and running on cmd. I am providing a simple code for connecting with the database and performing basic operations. I am getting a continuous exception - ClassNotFoundException when I'm using com.mysql.jdbc.Connection and when I use java.sql.DriverManager instead of that..I get another error with No suitable driver found for jdbc:.... Please help me out guyz..I'm stuck with this...I have done the same with Netbeans..it worked perfectly there...but not with cmd.
import java.sql.*;
public class JDBCExample
{
static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
static final String DB_URL="jdbc:mysql://localhost/company";
static final String USER="root";
static final String PASSWORD="root";
public static void main(String args[])
{
Connection conn=null;
Statement stmt=null;
try
{
Class.forName("com.mysql.jdbc.Driver"); //tried java.sql.DriverManager as well
System.out.println("Connecting to Database...");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/company","root","root");
System.out.println("Creating Statement...");
stmt=conn.createStatement();
String sql="Update login set password=123 where id=1";
Boolean ret=stmt.execute(sql);
System.out.println("Return value is: "+ret.toString());
int rows=stmt.executeUpdate(sql);
System.out.println("Rows impacted: "+rows);
sql="Select *from login;";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
String id=rs.getString("id");
String user=rs.getString("username");
String pass=rs.getString("password");
System.out.println("ID: "+id);
System.out.println("username: "+user);
System.out.println("password: "+pass);
}
rs.close();
stmt.close();
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(stmt!=null)
stmt.close();
}
catch(SQLException se2)
{
}
try
{
if(conn!=null)
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
}
System.out.println("End");
}
}