Hello everybody
I have a very simple question...how do include a jar file into my code? I am more used to c++ and I would just tell my code where to find the library I want and then I just use it.
I am using Ubuntu if that makes any difference.
This is really frustrrating me. I've looked on the web and found instructions that tell me to add -classpath option to the compile command line. I then use import in my Java code for the package inside the jar file i want...code compiles but I get a ClassNotFound exception.
Here is my code
NotConnectedException is an empty class.
I tried using NetBeans to help import things but this only made matter worse (the best I could out of NetBeans was it telling me my code was uncompiled and then it would not let me compile any files :S). I just want to be able to write my code in a text editor and compile from the command line. Any help would be much appreciated.
Thank you all so very much for your time
Andrew
I have a very simple question...how do include a jar file into my code? I am more used to c++ and I would just tell my code where to find the library I want and then I just use it.
I am using Ubuntu if that makes any difference.
This is really frustrrating me. I've looked on the web and found instructions that tell me to add -classpath option to the compile command line. I then use import in my Java code for the package inside the jar file i want...code compiles but I get a ClassNotFound exception.
Here is my code
Code:
import java.sql.*;
import org.hsqldb.*;
import org.hsqldb.jdbcDriver; //Probably dont need this?
public class Database
{
private Connection conn;
public Database(String un, String pw, String dbf) throws SQLException, ClassNotFoundException
{
Class.forName("org.hsqldb.jdbcDriver");
conn = DriverManager.getConnection("jdbc:hsqldb:file:" + dbf, un, pw);
}
public int update(String theSQL) throws SQLException, NotConnectedException
{
if(conn == null)
throw new NotConnectedException();
Statement stat = conn.createStatement();
return stat.executeUpdate(theSQL);
}
public ResultSet query(String theSQL) throws SQLException, NotConnectedException
{
if(conn == null)
throw new NotConnectedException();
Statement stat = conn.createStatement();
return stat.executeQuery(theSQL);
}
public void close() throws SQLException, NotConnectedException
{
if(conn == null)
throw new NotConnectedException();
update("SHUTDOWN");
}
}
Code:
import java.sql.*;
import org.hsqldb.*;
import org.hsqldb.jdbcDriver;
public class DatabaseTest
{
public static void main(String args[])
{
System.out.println("START OF PROG");
Database db;
ResultSet rs;
try
{
//Create a connection to the database
db = new Database("TestDB","sa","");
//Try and work with the Database making sure to catch any exception
try
{
//Create a table
db.update("CREATE TABLE animals (type VARCHAR(30))");
//Add some data
db.update("INSERT INTO animals VALUES('dog')");
db.update("INSERT INTO animals VALUES('cat')");
db.update("INSERT INTO animals VALUES('fish')");
db.update("INSERT INTO animals VALUES('bird')");
//Retreive the data
rs = db.query("SELECT * FROM animals");
//Loop through rs and out put data
while(rs.next())
{
String type = rs.getString(0);
System.out.println(type);
}
}
catch(NotConnectedException dbe)
{
System.out.println("OPPS! The application is not connected to a Database. This could mean the Database connection details are incorrect. Please confirm details and try again. If problems persist please contact you DBA.");
}
}
catch(SQLException sqle)
{
System.out.println("OPPS! SQL Error: " + sqle.getMessage());
}
catch(ClassNotFoundException ce)
{
System.out.println("OPPS! Class not found: " + ce.getMessage());
}
System.out.println("END OF PROG");
}
}
Code:
javac -classpath /media/ExpansionDrive/java_include/hsqldb.jar *.java
NotConnectedException is an empty class.
I tried using NetBeans to help import things but this only made matter worse (the best I could out of NetBeans was it telling me my code was uncompiled and then it would not let me compile any files :S). I just want to be able to write my code in a text editor and compile from the command line. Any help would be much appreciated.
Thank you all so very much for your time
Andrew