Swing form database connection - java

I am new to Java and NetBeans, and I am attempting to create a form that
connects to a database using JDBC connection
reads information from seven columns and displays them on a jTable component already on the form
I already have this working. I am now trying to optimize my code and use a better architecture to separate the database connection and the user interface (UI forms) code so that I can have a separate class to perform the connection and then simply call the method from this class in the code behind the button. The problem with using NetBeans and forms is that I don't know where to instantiate this class and such. Below is a cope of the class that I have created to perform the JDBC connection
public class ConnectionManager {
private static String url = "jdbc:mysql://localhost:3306/prototypeeop";
private static String driverName = "com.mysql.jdbc.Driver";
private static String username = "root";
private static String password = "triala";
private static Connection con;
private static String url;
public static Connection getConnection() {
try {
Class.forName(driverName);
try {
con = DriverManager.getConnection(url, username, password);
} catch (SQLException ex) {
// log an exception. fro example:
System.out.println("Failed to create the database connection.");
}
} catch (ClassNotFoundException ex) {
// log an exception. for example:
System.out.println("Driver not found.");
}
return con;
}
}
This is already a .java file. I have a JForm, and I need to call this method behind the button. Here is how I do it in the form currently without using a connection class:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model=(DefaultTableModel)jTable1.getModel();
model.setRowCount(0);
String sql="Select * from eopdata";
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/prototypeeop","root","jakamuga");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
String Year=rs.getString("Year");
String Month=rs.getString("Month");
String Day=rs.getString("Day");
String MJD=rs.getString("MJD");
Double xarcsec=rs.getDouble("xarcsec");
Double yarcsec=rs.getDouble("yarcsec");
Double UT1UTCsec=rs.getDouble("UT1UTCsec");
model.addRow(new Object[] { Year, Month, Day, MJD,xarcsec,yarcsec,UT1UTCsec});
}
}
catch(Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
How can I use the class instead of hard coding in the connection? I have already created the class but where do I instantiate it. Do I do it in the main of the form or do I do it in the actionevent code with the following code?
private Connection con = null;
private Statement stmt = null;
private ResultSet rs = null;
con = ConnectionManager.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery(sql);

To literally answer your question: your getConnection method is a public static method so you can call this from anywhere. Just call ConnectionManager.getConnection() where-ever you need that connection.
Some other remarks about your code:
You shouldn't query a database in the actionPerformed method. This method is called on the EDT, and doing a database query and looping over the results is a long-running task. Doing this task on the EDT will block your UI. Consult the Concurrency in Swing tutorial for more info about Swing and threading
Consider caching the Connection object
Do not forget to close your resources. If I remember correctly, a ResultSet must be closed afterwards. Do this in a finally block

Related

SQL connections dangling: Where am I not correctly closing up connections correctly?

I am building a basic java application to load some files into a mysql database. I am able to load the files up and populate my tables without any problems. However after speaking to someone who reviewed my code, I am apparently not correctly closing my connections and wasting resources. Where am I not closing up the connections? Have I done this incorrectly?
I am using the try-with-resources construct within my DbSinger class to execute prepared statements to my database, which should automatically close the connection so long as the AutoCloseable interface is implemented, which it is in the parent class of Db. The close() method however is never reached. The DbSinger is instantiated inside my main() and then runs it's single method populateSingers() with an ArrayList of Singer objects.
Connection Class
public class SQLConnection {
private static final String servername = "localhost";
private static final int port = 3306;
private static final String user = "ng_user";
private static final String pass = "ng";
private static final String db = "ng_music";
private static final String connectionString = "jdbc:mysql://" + servername + ":" + port + "/" + db;
public Connection provide() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(connectionString, user, pass);
}
catch (SQLException | ClassNotFoundException e) {
throw new SQLConnectionException(e);
}
}
public class SQLConnectionException extends RuntimeException {
SQLConnectionException(Exception e) {super(e);}
}
}
Abstract parent class
public abstract class Db implements AutoCloseable{
private Connection connection;
Db() {
SQLConnection sqlC = new SQLConnection();
this.connection = sqlC.provide();
}
#Override
public synchronized void close() throws SQLException {
if(connection != null) {
connection.close();
connection = null;
System.out.println("Connection closed");
}
}
Connection getConnection() {
return connection;
}
boolean checkIfPopulated(String query){
try {
PreparedStatement ps = getConnection().prepareStatement(query);
ResultSet rs = ps.executeQuery();
return !rs.next();
} catch (SQLException e) {
e.printStackTrace();
}
return true;
}
}
Concrete class to execute queries to database for singers table
public class DbSinger extends Db {
public DbSinger() {
super();
}
public void populateSingers(ArrayList<Singer> singers) {
String populateSingersQuery = "insert into ng_singers(name, dob, sex) values(?,?,?)";
if(!checkIfPopulated("select * from ng_singers")){
System.out.println("Singer Table is already populated");
return;
}
try (PreparedStatement ps = getConnection().prepareStatement(populateSingersQuery)) {
for (Singer s : singers) {
ps.setString(1, s.getName());
ps.setDate(2, java.sql.Date.valueOf(s.getDob()));
ps.setString(3, s.getSex());
ps.addBatch();
}
ps.executeBatch();
System.out.println("Singers added to table");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
My code is able to execute is able to run fine and does what it needs to, but I want to understand why and where I am not closing connections, and to understand how I can resolve this. Or at least understand if I am approaching this wrong.
In your case, you need to instantiate DBSinger class in try-with-resources statement to close the underlying connection.
Instead of doing:
DbSinger dbSinger = new DbSinger();
You need to do:
try (DbSinger dbSinger = new DbSinger()) {
// Your other code
}
This way the close() method you are overriding in your Db class will be called automatically.
Also, close the preparedStatement you created in your checkIfPopulated method by:
try (PreparedStatement ps = getConnection().prepareStatement(query)) {
// Other codes
}
Your code is old way. And you do need close manually. However, with Java 8, you can use try with resource like below,
try (Connection conn = ds.getConnection();
Statement stmt = conn.createStatement()) {
try {
stmt.execute(dropsql);
} catch (Exception ignore) {} // ignore if table not dropped
stmt.execute(createsql);
stmt.execute(insertsql);
try (ResultSet rs = stmt.executeQuery(selectsql)) {
rs.next();
} catch (Exception e2) {
e2.printStackTrace();
return("failed");
}
} catch(Exception e) {
e.printStackTrace();
return("failed");
}

Sqlite cannot insert data when database insider src folder

I spend like 5 hour re-writing all methods cause thought i make mistake while inserting data into DB BUT i noticed everything is fine when i got database outside src folder.
This is my code
public class DataBaseModel {
public static final String DRIVER = "org.sqlite.JDBC";
// public static final String DB_URL = "jdbc:sqlite::resource:sqlite/databse.db"; // insider src/sqlite
public static final String DB_URL = "jdbc:sqlite:database.db";
private Connection conn=null;
private Statement stat=null;
public DataBaseModel() {
try {
Class.forName(DataBaseModel.DRIVER);
} catch (ClassNotFoundException e) {
System.err.println("NO JDBC DRIVER");
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(DB_URL);
stat = conn.createStatement();
} catch (SQLException e) {
System.err.println("Connection problem");
e.printStackTrace();
}
createTables();
}
When i got my project setup like that:
public static final String DB_URL = "jdbc:sqlite:database.db";
everythin work fine:
creating records
display records form db
creating tables
But when i change to
public static final String DB_URL = "jdbc:sqlite::resource:sqlite/databse.db";
there is an error
Connection problem java.sql.SQLException: resource sqlite/database.db not found: java.net.MalformedURLException: no protocol: sqlite/database.db
I get rid of error unconsciously by copping database to src/sqlite thats why i was unable to see any error for all that time ...
Any idea what cause problem when database.db is inside src/sqlite?
I explained as much as i can.
Thx for help

Can I use the same connection object when calling within Methods?

I have got a program this way:
public void MethodOne()
{
String sqlquery = "select * from vendor_items where category_id = 1 ";
PreparedStatement consildatedPst = connection.prepareStatement(sqlquery);
ResultSet consilatedReslset = consildatedpst.executeQuery();
while(consilatedReslset.next())
{
String name = consilatedReslset.getString("name");
if(name!=null)
{
MethodTwo();
}
}
}
public void MethodTwo(String name)
{
String sqlquery2 = "select ename from Vendor where name=?";
PreparedStatement otherPst = connection.prepareStatement(sqlquery2);
otherPst.setString(1,name);
}
This is the way connection is established (Later I will go for Connection Pooling).
public class DBConnection {
public static Connection getDBConnection() {
String sURL="jdbc:mysql://localhost:3306/oms";
String sUserName="root";
String sPwd="";
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(sURL, sUserName,sPwd);
return conn;
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
}
My question is Can I use the same connection object when calling within Methods??
Yes, you can.
When you do:
connection.prepareStatement(sqlquery2);
It creates a new statement object using the same connection. So the ResultSets that you obtain from them will belong to different Statements and will be different and there will be NO PROBLEM for you.
In short: Different Statements manage different ResultSets. If you get 2 ResultSets from the same Statement when you get the second one the first one will be dropped but if you have 2 Statements you can manage 2 ResulSets without problem (while the connection is open, of course)
Only if you aren't using the connection in multiple threads or nesting your own methods. In other words, no. Use a new connection per method. To avoid overhead use a connection pool.

Java Swing Timer with SQL

I'm trying to setup a thread that loops every 100ms with every iteration querying a table in a SQL database. Here is what I have in my public static void main class. How can I define the connection outside of the listener and only call the query in the loop?
// Database credentials
final String url = "jdbc:mysql://192.168.0.0/";
final String db = "db";
final String driver = "com.mysql.jdbc.Driver";
final String table = "table";
public final Connection conn = null;
// Define listner
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
System.out.println("Reading Info.");
try {
Class.forName(driver);
try {
conn = DriverManager.getConnection(url+db,"root","pass");
Statement st = (Statement) conn.createStatement();
String sql = "";
st.executeUpdate(sql);
conn.close();
} catch (SQLException s) {
s.printStackTrace();
JOptionPane.showMessageDialog(null, "ERROR: Please try again!");
}
} catch (ClassNotFoundException cnfe){
JOptionPane.showMessageDialog(null, "ERROR:");
}
}
};
Timer timer = new Timer( 100 , taskPerformer);
timer.setRepeats(true);
timer.start();
Thread.sleep(100);
}
Right now it's giving me the following error:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
The final local variable conn cannot be assigned, since it is defined in an enclosing type
You don't want to. Connections are not thread safe per JDBC specification. Hence there is no reason or justification for Connection variable not being local to the thread. Why isn't it?
Now that you have an answer to your non-proximate issues, let me complement that with your proximate issue. You have this: public final Connection conn = null; which makes no sense. You have assigned null to a final variable. You may have added final because the compiler complained you are using a non-final variable in an inner class. What you probably want to achieve is a lazily initialized singleton. Remove the final modifier and then write a separate synchronized function that retrieves the connection and initializes it as needed:
private Connection conn;
private synchronized Connection connection() {
if (conn == null) conn = createConnection();
return conn;
}
Of course, you'd better make sure that you never use the connection concurrenly, but your use case doesn't call for that. Another issue with this naive approach to connection pooling are connections left in an invalid state.

oracle database connection in java

I am new to java and i have written the following:
package mypackage;
public class DBconnection {
Connection con = null;
public Connection getConnection() throws Exception, SQLException
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:#zzz:1521:zzz","zzz", "zzz");
}
catch(Exception e)
{
}
return con;
}
public void removeConnection() throws SQLException
{
con.close();
}
}
When I am accesing the above code in a jsp page i have written following:
DBconnection dbconnect = new DBconnection();
dbconnect.getConnection();
I want to erite dbconnect.con.prepareStatement......("sql query here");// but error is coming as con is not public in mypackage.DBconnection ; cannot be accessed outside package
dbconnect.con is not accessible why????? I have declared variable con in public in the above code. Error is coming as con is not public in mypackage.DBconnection ; cannot be accessed outside package, how to resolve this??? please help me
In the code posted above, con is package visible and not public. That is why you get the message cannot be accessed outside package. To make it public you have to write
public Connection con = null;
Note that if you are just going to call
dbconnect.con.prepareStatement
you might end up with a NullPointerException, since con is only initialized in the getConnection() method. So I would advise to use the getter you created
dbconnect.getConnection().prepareStatement
Of course, the getter would need to be adjusted to only create a new connection when con is still null. Otherwise it can just return con directly. The removeConnection should then after closing the connection set con to null, otherwise calling the getConnection after calling removeConnection would return a closed connection, which is pretty useless
move this code to the constructor
public DBconnection(){
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:#zzz:1521:zzz","zzz", "zzz");
}
catch(Exception e)
{
}
}
then make one object of it and share it to all your application classes and when you want to use connection just call getConnecion() from his object

Categories