I'm working on getting my database to talk to my Java programs. What do I need to get started?
Having already read through (and been thoroughly confused, something that does not happen often) with some other turorials I figured I'd best ask here. How do I import a jar file from the local directory? Can someone give me a quick and dirty sample program using the JDBC?
Following this answer gives me this.
SqlTest.java:16: cannot find symbolsymbol : class Connectionlocation: class SqlTest Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/projects", "user", "123" ); ^SqlTest.java:16: cannot find symbolsymbol : variable DriverManagerlocation: class SqlTest Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/projects", "root", "123" ); ^SqlTest.java:17: cannot find symbolsymbol : class Statementlocation: class SqlTest Statement st = conn.createStatement(); ^SqlTest.java:18: cannot find symbolsymbol : class ResultSetlocation: class SqlTest ResultSet rs = st.executeQuery( "select * from table" ); ^4 errors
I'm on OS X if that makes any difference.
Now I'm getting a rather stupendous error:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failureThe last packet sent successfully to the server was 0
milliseconds ago. The driver has not received any packets from the server. at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.
java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at
com.mysql.jdbc.Util.handleNewInstance(Util.java:409) at
com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122) at
com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2260) at
com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:787) at
com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49) at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.
java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at
com.mysql.jdbc.Util.handleNewInstance(Util.java:409) at
com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:357) at
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) at
java.sql.DriverManager.getConnection(DriverManager.java:582) at
java.sql.DriverManager.getConnection(DriverManager.java:207) at
SqlTest.main(SqlTest.java:22)Caused by:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failureThe
last packet sent successfully to the server was 0 milliseconds ago. The driver has not
received any packets from the server. at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.
java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at
com.mysql.jdbc.Util.handleNewInstance(Util.java:409) at
com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122) at
com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344) at
com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2181) ... 12 moreCaused
by: java.net.ConnectException: Connection refused at
java.net.PlainSocketImpl.socketConnect(Native Method) at
java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at
java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at
java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at
java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432) at
java.net.Socket.connect(Socket.java:529) at
java.net.Socket.connect(Socket.java:478) at java.net.Socket.<init>
(Socket.java:375) at java.net.Socket.<init>(Socket.java:218) at
com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256) at
com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:293) ... 13 more
Contents of the test file:
import com.mysql.jdbc.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SqlTest
{
public static void main(String [] args) throws Exception
{
// Class.forName( "com.mysql.jdbc.Driver" ); // do this in init
// // edit the jdbc url
Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/projects?user=user1&password=123");
// Statement st = conn.createStatement();
// ResultSet rs = st.executeQuery( "select * from table" );
System.out.println("Connected?");
}
}