• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

trying to get JDBC code to work

rookie1010

Senior member
hey i tried to run this java code after compiling it


// JDBC|Test ? complete code
public class JDBCTest {

public static void main(String args[]){

RunDB runDB = new RunDB();

try{
runDB.loadDriver();
runDB.makeConnection();
runDB.buildStatement();
runDB.executeQuery();
}catch(Exception e){
e.printStackTrace();
}

}
}


&


//RunDB
import java.sql.*;

public class RunDB {

Connection connection;
Statement statement;

public void loadDriver() throws ClassNotFoundException{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}

public void makeConnection() throws SQLException {
connection=
DriverManager.getConnection("jdbc: odbc: PurchaseOrder");
}

public void buildStatement() throws SQLException {
statement = connection.createStatement();
}

public void executeQuery() throws SQLException {

boolean foundResults =
statement.execute("SELECT * FROM Transaction");
if(foundResults){
ResultSet set = statement.getResultSet();
if(set!=null) displayResults(set);
}else {
connection.close();
}
}

void displayResults(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
int columns=metaData.getColumnCount();
String text="";

while(rs.next()){
for(int i=1;i<=columns;++i) {
text+="<"+metaData.getColumnName(i)+">";
text+=rs.getString(i);
text+="</"+metaData.getColumnName(i)+">";
text+="n";
}
text+="n";
}

System.out.println(text);

}

}


and when i tried to rn the code i get the following errors



E:\Work\programming\java\JavaPrograms>java rundb
Exception in thread "main" java.lang.NoClassDefFoundError: rundb (wrong name: Ru
nDB)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

E:\Work\programming\java\JavaPrograms>

what have i done wrong

(i got the code of the following site
http://www.developer.com/design/art...10925_3571661_3

i have got the code in this folder
E:\Work\programming\java\JavaPrograms

and the database in this folder
E:\Work\programming\java\database
 
Name your files RunDB.java and JDBCTest.java. Run the program with "java JDBCTest", your main method is not in the RunDB class.

Also:
1) Use StringBuffer or StringBuilder when concatenating strings in a potentially long loop.
2) Please use the Attach Code box.
3) Your link is broken.
 
thanks for the replies

i ran the program with "java JDBCTest" and am getting the following error

E:\Work\programming\java\JavaPrograms>java JDBCTest
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not fou
nd and no default driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at RunDB.makeConnection(rundb.java:14)
at JDBCTest.main(jdbctest.java:10)

E:\Work\programming\java\JavaPrograms>

i checked in the System DSN, and the database file does seem to have been added.

my link is http://www.developer.com/design/article.php/3571661

 
you mean in this one

DriverManager.getConnection("jdbc: odbc: PurchaseOrder");

no, i had the places in there but the bulletn board renders it as

DriverManager.getConnection("jdbc😱dbc😛urchaseOrder");
 
hey i finally got it to work,

i needed to add the file to user DSN database, adding it to the System DSN or the File DSN did not work, interestingly eough the article stated that i needed to add it to the File DSN
 
Back
Top