Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm making a java Project that going to display a Request "something :)" from database..
I've done making a database and a request form.
My problem is I don't have an idea how to display the message on my JPANEL.
Here's the code.
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/webservice","root","");
String sql="Insert into request(PurposeofTrip,DestinationS,VehicleModel,RequestingDept,DriverName,OvernightUse) values (?,?,?,?,?,?)";
pst=conn.prepareStatement(sql);
pst.setString(1, txt_trip.getText());
pst.setString(2, txt_dest.getText());
String value=combo_model.getSelectedItem().toString();
pst.setString(3, value);
pst.setString(4, txt_requ.getText());
pst.setString(5, txt_dname.getText());
String value2=combo_night.getSelectedItem().toString();
pst.setString(6, value2);
pst.execute();
JOptionPane.showMessageDialog(null, "Request Send");
}catch(Exception e){
...
}
How to display a message in JPanel from DATABASE?
Since you're inserting into the database, it looks like you want to show the status of the insertion. What I would do is just use a JLabel. No need for a JPanel in a JPanel you need to actually paint the message yourself. You can do something like this. Keep in mind that .excuteUpdate() return an int which is the number of rows affected.
int result = pst.executeUpdate();
if (result == 0) {
statusLabel.setForeground(Color.RED);
statusLabel.setText("Error Inserting record");
} else {
statusLabel.setForeground(Color.GREEN);
statusLabel.setText("Insert Successful");
}
statusLabel being a JLabel that you have added somewhere to your GUI previously
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Good morning . I have a question . I have two screens in my program one is check in that will have special field (check no).The other screen is check out. It also have check no . I want to write a query that relate the check in with check out by using the check no and save it in one row in database . i wrote :
public void actionPerformed(ActionEvent arg0) {
while(textField.getText()==Securityoffcheck.getnumber())
{
try{
String timeStamp = new SimpleDateFormat("dd.MM.yyyy HH.mm.ss").format(new Date());
String sql = "INSERT INTO RECORD(CHECK_OUT)VALUES(?)";
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(12, timeStamp);
pst.execute();
}
catch (Exception e) {
JOptionPane.showMessageDialog(null,e);
}
}
}
});
but nothing happen .
Check your parameter index in pst.setString(12, ...). For your SQL statement INSERT INTO RECORD(CHECK_OUT) VALUES(?) you should set the parameter index to 1.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
when am running my action i faced Error :
" java.util.ArrayList cannot be cast to java.lang.String "
and this is my code :
textField_1 = new JTextField();
textField_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object selected = list_1.getSelectedValue();
Connection conn = null;
PreparedStatement stmt = null;
String a =null;
try{
conn=DriverManager.getConnection("jdbc:mysql://localhost/flyer","root","000");
String query= " INSERT INTO flyer_item (discount) SELECT price * ? FROM `item` WHERE item_name = `?` ";
Statement st= conn.createStatement();
java.sql.PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1,a);
ps.setString(2,(String) selected);
ps.executeUpdate();
st.executeUpdate(query);
} catch (SQLException se){
System.out.println(se.getMessage());
} }} );
Any help please ?
Here Selected is an array-list and you are trying to convert it into string which is not possible:
ps.setString(2,(String) selected);
What you should do?
ps.setString(2, selected.tostring());
Are you sure about this line ?
Object selected = list_1.getSelectedValue();
Make sure selected is a String before casting . You can use instanceof operator to check the Object type.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm writing a Java Swing program for a college library. This program is connected to an MS Access database.
The program has a JTextField where the "Number" has to be entered. Below the TextField, there's a JButton. After the number is entered and the button is clicked, I want the program to print out the respective "Department"
For example :
When the user types the Number "3" in the JTextField and clicks the 'Enter'
button, I want my program to print out "Computers" i.e the data from the adjoining cell.
How do I achieve this? Thanks in advance!
Start by taking a look at JDBC Database Access.
You will need a JDBC driver for MS Access, UCanAccess is reasonable popular.
You will then need to:
Load the JDBC driver
Connect to the database
Execute a SQL query which can select the data you want based on your critera
Retrieve the data from the resulting data set
For example...
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
try (Connection conn = DriverManager.getConnection("jdbc:ucanaccess://<mdb or accdb file path>", user, password)) {
try (PreparedStatement stmt = conn.prepareStatement("SELECT department FROM Table1 WHERE ID = ?")) {
stmt.setInt(1, 3); // Use a variable for the ID
try (ResultSet rs = stmt.executeQuery()) {
// We're only expecting a single row...
if (rs.next()) {
String department = rs.getString(1);
}
}
}
} catch (SQLException exp) {
exp.printStackTrace();
}
} catch (ClassNotFoundException exp) {
exp.printStackTrace();
}
You might also want to take a look at a SQL Tutorial
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
`String query = "select count(book_id) as book_id from library_books_details where book_id = ?";`
PreparedStatement psEnd = collegeCon.prepareStatement(query);
psEnd.setInt(1, bookId);
ResultSet rs = psEnd.executeQuery();
if (rs.next()) {
int count= rs.getInt(1);
System.out.println("Count= " + count);
} else {
System.out.println("error: could not get the record counts");
}
as per http://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html
The flow of database access is
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String strColumn = rs.getString("....");
int intColumn= rs.getInt("....");
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have two kind of methods to remove JTable selected row.
I create this methods in my GUI Class:
First:
public void dellAction() {
if (table.getSelectedRow() > -1) {
int rowToDelete = table.getSelectedRow();
int rowToModel = table.convertRowIndexToModel(rowToDelete);
Object rowId = table.getValueAt(rowToModel, 0);
try {
Connection con;
PreparedStatement ps = null;
con = DriverManager.getConnection(...);
ps = con.prepareStatement("delete from table where id=?");
ps.setObject(1, rowId);
if (ps.executeUpdate() == 1) {
model1.removeRow(rowToModel);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
} else JOptionPane.showMessageDialog(null, "Select A Row");
}
Second:
public void delete(DefaultTableModel model, int modelRow) {
if (table.getSelectedRow() > -1) {
Object rowId = model.getValueAt(modelRow, 0);
try {
Connection con;
PreparedStatement ps = null;
con = DriverManager.getConnection(...);
ps = con.prepareStatement("delete from table where id=?");
ps.setObject(1, rowId);
if (ps.executeUpdate() == 1) {
model.removeRow(modelRow);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "Select A Row");
}
}
The question depends on the context of your application. In a perfect world, your TableModel would be modelling data from some kind of factory/controller that was responsible for managing the data, which would read/write to some kind of data source.
This would allow your TableModel the opportunity to simply not care where the data was coming from or going to, only that it had some means of performing these actions.
The same would go for your JTable, it should have no idea about the source of the data, only that the TableModel provides the required contract it needs to fulfill its responsibility.
This, then, raises the question of, who should actually perform what jobs.
In this scenario, I would provide some means for the factory/controller to alert registered listeners to changes. This would decouple the API in such away that any part of the program would then be able to modify the factory/controller without needing to know about everybody else who might be using that factory/controller, but still be able to react to those changes.
So, my answer would generally be neither...but...your second one comes closest to achieving this, but I'm concerned about the need to extract data from the model in this way, but that's me...
This is, of course, is just my opinion, based on the factory, observer, producer-consumer and model-view-controller patterns