I am quite new to Java and MySQL. I had followed this video https://www.youtube.com/watch?v=BCqW5XwtJxY&t=557s which allows me to connect localhost mysql to Java but it seems like not working for my code and project. This is my code.
DPAT.java
public static void main(String[] args) {
DBConnection connect = new DBConnection();
connect.getData();
}
DBConnection.java
package dpat;
import java.sql.*;
public class DBConnection {
private Connection con;
private Statement st;
private ResultSet rs;
public DBConnection()
{
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:1234/databasetest","root","");
st = con.createStatement();
}
catch(Exception ex)
{
System.out.println("erro:" + ex);
}
}
public void getData()
{
try{
String query = "select * from tabletest";
rs = st.executeQuery(query);
System.out.println("Records from databases");
while(rs.next())
{
int ID = rs.getInt("ID");
String name = rs.getString("Name");
int age = rs.getInt("Age");
System.out.println(" Name: " + name + " ID " + ID + " age " + age );
}
}
catch(Exception ex)
{
System.out.println(ex + "error");
}
}
}
May I know what is wrong with my code? Thank you very much! :)