I have a table called Rooms and this how the table looks like
CREATE TABLE Rooms
(
id INT AUTO_INCREMENT,
RoomNumber VARCHAR(32) NOT NULL,
AvailableSeats INT NOT NULL,
PRIMARY KEY (id)
)
In my code below, i am able to fill the combo box with the room numbers in the database. But what i want to do is, when i select a room number, the available seats for that room number must update.
For instance, when i select
RoomNumber 4, available seats 4
RoomNumber 3, available seats 2
How can i achieve this ?
Query
// query to fetch room numbers to combo box
public void FindRoom() {
String query = "Select * from Rooms";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs.next()) {
String getRoomNumber = rs.getString("RoomNumber");
FindRoom.addItem(getRoomNumber);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
//query to get respective available seats for every room number
public void FindSeats() {
String query = "Select * from Rooms where RoomNumber = "+FindRoom.getSelectedItem();
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs.next()) {
String getAvailableSeat = rs.getString("AvailableSeats");
seats.setText(getAvailableSeat);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
Related
I want to Count all the rows in the table using java and display the count of all the rows in the textfield. I need the count of employee from the table. I have attached below the code and the query used to achieve this. I received the error of the below code Column 'id' not found. error displayed
public void Customer()
{
try {
pst = con.prepareStatement("SELECT COUNT(*) FROM customer");
ResultSet rs = pst.executeQuery();
while(rs.next())
{
String id1 = rs.getString("id");
txtmsg.setText(id1);
}
} catch (SQLException ex) {
Logger.getLogger(gsm.class.getName()).log(Level.SEVERE, null, ex);
}
}
There clearly is no "id" column in your select. You could either get the result by column number like so:
int count = rs.getInt(1);
Or you could use an alias for the column and get result by that name, eg.:
pst = con.prepareStatement("SELECT COUNT(*) AS customerCount FROM customer");
int count = rs.getInt("customerCount");
First I have table Appointment_Table with columns Appointment_ID,Doc_ID,Dep_Id,SchedDate,Patient_ID and Patient_Table with Patient_ID,FName,MI,LName, and so on.... and Doctor_Table with Doc_ID,FName,MI,LName and so on... so that I created a frame Add Appointments frame with 3 JComboBox first is Department Name and it contains of course the Deparment Name and then the Doctor Names inside it contains FName,Mi,LName and same for the Patient Name.
So that I insert all departments name in JCombobox that only have a doctor
public void ViewDepartmentName(){
try{
String sql = "Select DISTINCT Department_Name from Department_Table\n" +
"inner join User_Table on Department_Table.Department_ID=User_Table.Department_ID\n" +
"where Role_ID = 3";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next()){
String add1 = rs.getString("Department_Name");
DoctorDep.addItem(add1);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally {
try {
rs.close();
pst.close();
}catch(Exception e){
}
}
}
so whenever I click the JComboBox of Department name it will show only the Department name that have a doctors
and also for the Patient Name
private void ViewDoctorPatientsBox(){
try{
String sql = "Select * from Patient_Records";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next()){
String add1 = rs.getString("First_Name");
String add2 = rs.getString("MI");
String add3 = rs.getString("Last_Name");
DoctorPatient.addItem(add1+" "+add2+" "+add3);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally {
try {
rs.close();
pst.close();
}catch(Exception e){
}
}
}
and in Patient Name I insert the Fname,MI,Lname like so showed in the code above
same with the Doctor Names it only show when I select their Departments like so
try{
String sql = "Select * from User_Table\n" +
"inner join Department_Table on User_Table.Department_ID=Department_Table.Department_ID\n" +
"where Department_Name = ? AND Availability = 1";
pst = conn.prepareStatement(sql);
pst.setString(1, (String)DoctorDep.getSelectedItem());
rs = pst.executeQuery();
DoctorNames.removeAllItems();
DoctorNames.addItem("Select");
while(rs.next()){
String add1 = rs.getString("First_Name");
String add2 = rs.getString("MI");
String add3 = rs.getString("Last_Name");
DoctorNames.addItem(add1+" "+add2+" "+add3);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally {
try {
rs.close();
pst.close();
}catch(Exception e){
}
}
after I select all I want to select I need to save it to Appointment_Table that only their Doc_ID,Dep_ID,Patient_ID but the JComboBox have the name what syntax should I do?
that only their Doc_ID,Dep_ID,Patient_ID but the JComboBox have the name what syntax should I do?
Your combo box needs to store two pieces of data:
the ID
the name.
So you need to create a custom object to store in the combo box and then you need to only display the name in the combo box.
There are two approaches to displaying the name:
Create a custom renderer. This is the better approach but is a little more involved. See Combo Box With Custom Renderer for an example of this approach.
Override the toString() method of your custom object to simply return the name. The default renderer for the combo box will invoke the toString() method of the object. See Combo Box With Hidden Data for more information on this approach.
I created a sequence for id and I need max id from the table. So, I am using autoGeneratedKeys(), but it's not working.
public int createDataDAOImplentation(UserData c){
int i=0; String sql="";
try
{
ds.setPs("insert into contact(id,firstName,lastName,email,dob,gender)values(contact_seq.nextval,?,?,?,?,?)");
ds.getPs().setString(1, c.getFirstname());
ds.getPs().setString(2, c.getLastName());
ds.getPs().setString(3, c.getEmail());
ds.getPs().setint(4, c.getDob());
ds.getPs().setLong(5, c.getGender());
ds.getPs().executeQuery();
ResultSet rs = ds.getPs().getGeneratedKeys();
System.out.println("iam");
if(rs.next()){
i = rs.getInt(1);
System.out.println("in"0);
}
ds.getCon().commit();
ds.getCon().close();
}
catch(SQLException ex)
}
my textfield is called pruebamax
With this function I make the connection with the database
public ResultSet getmax() {
ResultSet r = null;
try {
String sql = "select count(*) as Cantidad from tbl_padre";
System.out.println(sql);
Statement st = cx.createStatement();
r = st.executeQuery(sql);
System.out.println(st.executeQuery(sql));
} catch (SQLException ex) {
Logger.getLogger(Tmrptryone.class.getName()).log(Level.SEVERE, null, ex);
}
return r;
}
his method in the event of a button, with this method I want to print in the textfield the data I receive from the database but I got an error.
public void actualizatext() {
try {
ResultSet r = mrp.getmax();
if (r.next()) {
String ID = r.getString("Cantidad");
pruebamax.setText(ID);
}
} catch (SQLException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
Now, I don't know what pruebamax means but the SQL statement you used:
String sql = "SELECT COUNT(*) AS Cantidad FROM tbl_padre";
is specifically used to count the total number of records currently maintained within the specified database table (tbl_padre). The value from that count will be held in the specified temporary field named: Cantidad. When you Use the SQL COUNT statement you are not going to be returned a String data type value. You will be expected to try and acquire a Integer value.
It will not acquire a value from your table ID field as what it looks like you expect.
To properly retrieve the records count from your applied SQL string then it should be used in this fashion:
int count = 0;
try {
String sql = "SELECT COUNT(*) AS rCount FROM tbl_padre";
Statement st = cx.createStatement();
ResultSet r = st.executeQuery(sql);
while (r.next()) {
count = r.getInt("rCount");
}
r.close();
st.close();
// Close your DB connection as well if desired.
// yourConnection.close();
//To apply this value to your JTextField:
pruebamax.setText(String.valueOf(count));
System.out.println("Total number of records in the tbl_padre " +
" table is: " + count);
}
catch (SQLException ex) {
ex.printStackTrace();
}
Try not to use actual table field names for temporary field names.
If you want to be more specific with your count then your SQL statement must be more specific as well. For example, let's assume that we want to count the number of records maintained in our table where a field named Age contains a value which is greater than 30 years old, our sql statement would look like this:
String sql = "SELECT COUNT(*) AS rCount FROM tbl_padre WHERE Age > 30";
You will of course have noticed the use of the SQL WHERE clause.
I'm trying to find out if data from my DB matches user input. I have tried this code, but its not doing what I need it to do. I would like it to display a message saying whether or not they match.
try {
String find = BC.getText(); //Get text from Textfield
String sql = "select * from Inventory where Barcode=?";
st = con.prepareStatement(sql);
rs = st.executeQuery();
while (rs.next()) {
if("Barcode".equals(find))
{
JOptionPane.showMessageDialog(null,"Matching");
}
else JOptionPane.showMessageDialog(null,"not matching");
}
} catch (Exception ex) {
}
Put the parameter that you want to search.See this;
String find = BC.getText(); //Get text from Textfield
String sql = "select * from Inventory where Barcode=?";
st = con.prepareStatement(sql);
st.setString(1, "12345678");//Set Barcode value.
rs = st.executeQuery();