Reputation: 109
I'm trying to get category name from combobox and then insert it to my database
This is my code, but I don't know how to write the code to make this work. Any ideas ?
The below code is my add button (trying to make work String value, String query). However, the code is really wrong I think.
String value=jComboBox_Category2.getSelectedItem().toString();
String qquery="INSERT INTO Products ( Cat_products) VALUES ('"+Cat_products.getText()+" ') ";
String query="INSERT INTO Products(Pro_Id ,Pro_Name,Pro_Price,Pro_Quantity,Pro_Supplier_id,Pro_Tax)VALUES ('"+Pro_Id.getText()+" ','"+Pro_Name.getText()+" ','"+Pro_Price.getText()+" ','"+Pro_Quantity.getText()+" ','"+Pro_Supplier_id.getText()+" ','"+Pro_Tax.getText()+" ') ";
executeSQLQuery(query,"Inserted");
Here is the code that my other elements get the data. So where do I have to write the code? And how should it be like?
public ArrayList<Update_del_insert_products> getproList() {
ArrayList<Update_del_insert_products> proList =new ArrayList<Update_del_insert_products> ();
Connection connection =getConnection();
String query ="SELECT * FROM Products";
Statement stt;
ResultSet rss;
try{
stt = connection.createStatement();
rss = stt.executeQuery(query);
Update_del_insert_products update_del_insert_products ;
while(rss.next()) {
update_del_insert_products = new Update_del_insert_products (rss.getString("Pro_Id"),rss.getString("Pro_Name"),rss.getString("Pro_Price"),rss.getString("Pro_Quantity"),rss.getString("Pro_Supplier_id"),rss.getString("Pro_Tax"));
proList.add(update_del_insert_products);
}
}catch (Exception e){
e.printStackTrace();
}
return proList;
}
Upvotes: 0
Views: 61
Reputation: 51
String qquery="INSERT INTO Products (Cat_products) VALUES (Cat_products.getText()");
Connection connection = (see below)
Statment stmt = connection.createStatement();
stmt.executeUpdate(qquery)
But you can concatenate insert statements if you use the same table
In order to retrieve data from database, first of all have to set the connection properly
Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=YourDatabseName;integratedSecurity=true;")//without authentication
You should not use * in sql statement. Make setter and getter methods in Update_del_insert_product class (Auto-generated methods) and use them.
while(rss.next()){
update_del_insert_products product = new Update_del_insert_products();
int i = 1
product.setPro_ID(rss.getString(Pro_ID, i++));
product.setPro_Name(rss.getString(Pro_Name, i++));
...
prolist.add(product);
}
Upvotes: 1