Troubleshooting Java Connection to MySQL
About a week ago posted some Java code in the #experts channel. David Busby This technote will explain the steps needed to make it work and how it has been fully tested against MySQL installed in the previous blog from November 2018. First of all the code:
// Sample from #experts import java.sql.*; class MysqlCon { public static void main(String args[]) { try { Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://192.168.33.10:3307/sonno","r
- ot","Passw0rd!");
//here sonno is the database name, root is the username and password Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery("select * from emp"); while(rs.next()) System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)); con.close(); } catch(Exception e) { System.out.println(e); } } }
The Java compiler is not installed by default on the CentOS VM, so one will need to run something like this as : root
yum install java-1.8.0-openjdk-devel.x86_64
After Java compiler is installed it is time to attempt to compile the code above. javac There should be a .java file with the same name of the class: . MysqlCon.java Note Java is case sensitive so make sure the file and class name have an exact match.
javac MysqlCon.java
The prompt should return silently and the compiler should have by then created a file with the same name. .class