0

When I create a new project in Netbeans, I added the sqlite.jar to the classpath and library. I wrote a short test program and it works great!

Now I want to add a GUI as a front end, so I create a brand new project in Netbeans but I create a swing project and create the GUI using netbeans. The GUI works fine by itself (just taking input and writing it to the console as a test).

So now in the GUI project I go and in the "Source Package" folder I add a new java class and I copy/paste the code from my other (working) sqlite implementation. I also add sqlite to the Classpath/library and nothing works!

This is (part) of the code I have:

package my.test; import java.sql.*; public class sqlaccess { Class.forName ("org.sqlite.JDBC"); Connection conn = DriverManager.getConnection("jdbc:sqlite:TestUser.db"); Statement stat = conn.createStatement(); /* commented out the connecting/creating tables etc code */ } 

The error I get is this:

C:\Users\xxx\Documents\NetBeansProjects\TestUser\src\my\testuser\sqlaccess.java:12: <identifier> expected Class.forName ("org.sqlite.JDBC"); C:\Users\xxx\Documents\NetBeansProjects\TestUser\src\my\testuser\sqlaccess.java:12: illegal start of type Class.forName ("org.sqlite.JDBC"); 

So I'm pretty sure the problem is the way I combine these 2 classes/files since each one works perfect stand alone.

2 Answers 2

2

The problem is this code block:

Class.forName ("org.sqlite.JDBC"); Connection conn = DriverManager.getConnection("jdbc:sqlite:TestUser.db"); Statement stat = conn.createStatement(); 

You need to move this code into a method, a constructor, a static initializer, or something similar. Code cannot be placed directly into the class body in Java.

Sign up to request clarification or add additional context in comments.

Comments

1

How about ..

public class sqlaccess { void init(){ try{ Class.forName ("org.sqlite.JDBC"); Connection conn = DriverManager.getConnection("jdbc:sqlite:TestUser.db"); Statement stat = conn.createStatement(); /* commented out the connecting/creating tables etc code */ } catch(ClassNotFoundException ex){ //Handle Exception } } } 

4 Comments

This gives the following error: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown Class.forName("org.sqlite.JDBC");
Wrap that line in a try { ... } catch (ClassNotFoundException e) { ... }
Edited as recommended by Haakon.
+1 nice catch. I stared at that forever and didn't see it...which explains why I've made that bonehead before...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.