So, I wrote the following program to connect to my local mysql server and create a table in the existing "test" database.
The program compiled without any errors, but I got the following run-time error:
java.sql.SQLException: Cannot connect to MySQL server on localhost:3306. Is there a MySQL server running on the machine/port you are trying to connect to? (java.lang.NumberFormatException)
at org.gjt.mm.mysql.Connection.connectionInit(Unknown Source)
at org.gjt.mm.mysql.jdbc2.Connection.connectionInit(Unknown Source)
at org.gjt.mm.mysql.Driver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:200)
at DB.main(DB.java:10)
Here's the code.
import java.sql.*;
public class DB
{
public static void main(String args[])
{
try
{
Class.forName("org.gjt.mm.mysql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
Statement st = con.createStatement();
st.executeUpdate("create table s1(name varchar(20), roll int(10))");
st.close();
con.close();
con.commit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}