pshortland
asked on
Full code to connect from Java to Access on same machine
Going mad here !
Can anyone post some exact and complete code that connects from Java to an Access database ?
I have a db called mydb.mdb and an ODBC setup as mydsn.
Then a table called mytable with a column called mycolumn.
All i need is the simplest code possible that merely shows one result from that column.
Anyone help ?
Can anyone post some exact and complete code that connects from Java to an Access database ?
I have a db called mydb.mdb and an ODBC setup as mydsn.
Then a table called mytable with a column called mycolumn.
All i need is the simplest code possible that merely shows one result from that column.
Anyone help ?
That's assuming you know how to connect. If you don't, then look at the code here:
http://www.exampledepot.com/egs/java.sql/ConnectOracle.html
Change the URL to "jdbc:odbc:mydsn" and the driver to "sun.jdbc.odbc.JdbcOdbcDri ver" and you should be good to go....
http://www.exampledepot.com/egs/java.sql/ConnectOracle.html
Change the URL to "jdbc:odbc:mydsn" and the driver to "sun.jdbc.odbc.JdbcOdbcDri
There is a sample right at the start of the JDBC tutorial as well:
http://java.sun.com/docs/books/tutorial/jdbc/overview/index.html
http://java.sun.com/docs/books/tutorial/jdbc/overview/index.html
ASKER
My code is below but will not work, says it cant find the main class...
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
public class DatabaseConnection
{
public static void main(String [] arguments)
{
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try
{
// Load the database drievrs. An ODBC: JDBC bridge
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
System.out.println("driver s loaded");
// Obtain connection to the database.
connection = DriverManager.getConnectio n("jdbc:od bc:mydb", "", "");
System.out.println("Connec tion established");
// Cerate statement to read and write to/from database.
statement = connection.createStatement ();
System.out.println("Statem ent created");
rs = statement.executeQuery("SE LECT * FROM mytable");
// Get next row.
rs.next();
System.out.println("data is: " + rs.getString("ename"));
// Close statement and conenction.
statement.close();
connection.close();
}
catch (ClassNotFoundException cnf)
{
System.out.println("Could not find class: " + cnf);
}
catch (SQLException sqle)
{
System.out.println("SQL problem: " + sqle);
}
}
}
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
public class DatabaseConnection
{
public static void main(String [] arguments)
{
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try
{
// Load the database drievrs. An ODBC: JDBC bridge
Class.forName("sun.jdbc.od
System.out.println("driver
// Obtain connection to the database.
connection = DriverManager.getConnectio
System.out.println("Connec
// Cerate statement to read and write to/from database.
statement = connection.createStatement
System.out.println("Statem
rs = statement.executeQuery("SE
// Get next row.
rs.next();
System.out.println("data is: " + rs.getString("ename"));
// Close statement and conenction.
statement.close();
connection.close();
}
catch (ClassNotFoundException cnf)
{
System.out.println("Could not find class: " + cnf);
}
catch (SQLException sqle)
{
System.out.println("SQL problem: " + sqle);
}
}
}
The code looks correct. To make it better, you can move the closing of the statement and connection to a finally block after the try blocks:
finally
{
try
{
if ( connection != null )
connection.close () ;
}
catch ( Exception e )
{
}
}
If the class is not found, you need to set the classpath. Try:
java -cp . DatabaseConnection
finally
{
try
{
if ( connection != null )
connection.close () ;
}
catch ( Exception e )
{
}
}
If the class is not found, you need to set the classpath. Try:
java -cp . DatabaseConnection
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If the ODBC name of the database is mydsn, then that is the name you should use in your JDBC-ODBC connection:
connection = DriverManager.getConnectio n("jdbc:od bc:mydsn", "", "");
It will have to be defined as a System DSN to be accessed by your program (not User DSN).
connection = DriverManager.getConnectio
It will have to be defined as a System DSN to be accessed by your program (not User DSN).
>> "jdbc:odbc:mydsn"
Already mentioned :)
>> It will have to be defined as a System DSN to be accessed by your program (not User DSN)
I think it works with User DSN too - at least for J2SE applications running under your logged-in account....
Already mentioned :)
>> It will have to be defined as a System DSN to be accessed by your program (not User DSN)
I think it works with User DSN too - at least for J2SE applications running under your logged-in account....
http://www.exampledepot.com/egs/java.sql/GetRsData.html