Note that there are some explanatory texts on larger screens.

plurals
  1. POJava program will not connect to mySQL database
    primarykey
    data
    text
    <p>Using a simple example online, I am running a Java program to return users from a table.</p> <p>The database is MySQL. I have already downloaded the JDBC driver, stored it in my JRE7 /lib folder.</p> <p>The problem is the program never connects to the database, it skips the whole try block and prints out a "test" string at the end. Have I overlooked a major step in this? It's really frustrating!!</p> <p>EDIT: Best to describe how I've attempted to set up a connection to the database.</p> <p>The mysql-connector-java-5.1.18-bin is stored in jre7/lib.</p> <p>I startup mysql workbench. The connection is name ive created is com.fyp.db, with the host address at 127.0.0.1:3306.</p> <p>I then run my code, which attempts to query the sample table - sakila - provided by mySQL workbench and..nothing!</p> <p>Installed MySQL workbench. Installed JDBC driver. </p> <pre><code>public class Version { public static void main(String[] args) { Connection con = null; Statement st = null; ResultSet rs = null; //String url = "jdbc:mysql://127.0.0.1/sakila"; String url = "jdbc:mysql://localhost:3306/sakila"; String user = "root"; String password = "root"; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager.getConnection(url, user, password); st = con.createStatement(); rs = st.executeQuery("select * from ACTOR;"); System.out.println("test"); if (rs.next()) { System.out.println(rs.getString(1)); } } catch (SQLException ex) { ex.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } if (st != null) { st.close(); } if (con != null) { con.close(); } } catch (SQLException ex) { ex.printStackTrace(); } } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload