It's 1:37am here so I'm greeting you all a good day.
I've got a problem here and I'm really really new to java. Please have patience on me. :(
I've got a .jar file that I imported to my Libraries in my netbeans project. It's called dbconnect.jar. I want to create a statement in my Fruits.java that will change my stmt
statement (from inside dbconnect.jar) to whatever mysql statement i want it to be (specifically, I want to add fruits to my db). Here's my project map:
So inside my dbconnect.jar contains the class Dbconnect
. Here are the contents of Dbconnect
:
package dbconnect;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Dbconnect {
public Connection conn = null;
public Statement stmt = null;
public ResultSet rs = null;
public Dbconnect(){
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String Host = "jdbc:mysql://localhost/dbname";
String Username = "root" ;
String Password = "";
conn = DriverManager.getConnection(Host, Username, Password);
stmt = conn.createStatement();
}catch(Exception e){
e.printStackTrace();
}
}
}
By the way, I'm using xampp for Apache and Mysql.
So my questions are, how do I call/import this dbconnect.jar
of mine so I can use it in Fruits.java
(and if possible, am I able to use extends
here?) and how do I make it so that I can edit the stmt
part in dbconnect.jar
using Fruits.java
?
This is currently what's inside the add button from my UI in Fruits.java:
I really need help with this. Thank you so much in advance! Oh and I'm still a starter. So I hope there will be no advanced codes. Thanks again!
And yes, I'm using JFrame. Here's what it looks like for now.
Since, you have Dbconnect.jar
on your classpath you can just say something like this in your Fruit.java
class:
Dbconnect db = new Dbconnect();
This will create a new object of Dbconnect
class with name db
.
In order for you to be able to edit the statement you'll have to create a new method and not just call it in the constructor.
Here is an example:
public void connect(String myStatement) {
/* Do Something */
}
You can then call connect()
function on db
object you created before.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.