How to show the whole column in a text area? - java

I want to show the whole column in the text area from the SQL database but in the TextArea it's only showing last row data. What is the solution?
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:oci8:#localhost:1521:XE","tushar","yahoo123");
st=con.createStatement();
rs=st.executeQuery("select CUSTOMER_ID from demo_customers" );
while(rs.next())
{
String CUSTOMER_ID = rs.getString("CUSTOMER_ID");
t2.setText("ID: " + CUSTOMER_ID); //JTextArea t2=new TextArea();
}
st.close();
con.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}

Instead of setting your text to a new value for each row, you should append the new text to the one you previously had. Adding linebreaks might be a good idea as well:
while(rs.next()) {
String CUSTOMER_ID = rs.getString("CUSTOMER_ID");
t2.append("ID: " + CUSTOMER_ID+"\n");
}

Related

How to display MySQL table data onto JTextPane as per JTextField ID

I have a MySQL table named assp1 with a field named, StudentID, a field named Maths and another maths_percentage. I also have designed a NetBeans GUI which is shown above, with a JTextField named as jtxtStudentID and a JTextPane named jtxtTranscript. I also have a JButton on the NetBeans GUI. What I want to do is, when I enter a particular studentID in the JTextField jtxtStudentID and click on the JButton, I want the Maths and maths_percentage data on assp1 displayed on the JTextPane jtxtTranscript, which is under the Result section of the GUI. The data to be displayed must depend on the StudentID entered into the JTextField. Please help me achieve that. Thank you. Below is my code. When I run it, it gives java.lang.NullPointerException.
private void jbtnTranscriptsActionPerformed(java.awt.event.ActionEvent evt) {
jtxtTranscript.setText(null);
StyledDocument doc=(StyledDocument) jtxtTranscript.getDocument();
Style style= doc.addStyle("StyleName", null);
try {
if(jtxtStudent!=null){
Connection conn=MySqlConnection();
Statement stmt = conn.createStatement();
ResultSet rs=null;
String qry=null;
qry=" SELECT maths_percentage FROM assp1\n"
+ " where StudentID=?";
PreparedStatement ps = null;
ps.setInt(1, Integer.parseInt(jtxtStudent.getText()));
rs=stmt.executeQuery(qry);
while(rs.next()){
int mathPerc=rs.getInt("maths_percentage");
doc.insertString(doc.getLength(), "\n\t\t STUDENT ACADEMIC REPORT : Assessment Period 1\t\n"
+ "\t\t\t\t"+mathPerc+"\n"
, style);
}
}else{
JOptionPane.showMessageDialog(null, "error retrieving % from database");
}
} catch (BadLocationException ex) {
Logger.getLogger(Term2.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Assp1.class.getName()).log(Level.SEVERE, null, ex);
}

JComboBox ExecuteQuery selecting Items error

I have 3 jComboBox. The first one is for the Room Type. When I select Room Type on the first jComboBox it must show in the second jComboBox all the available room, but when I select one of the Room Type, an error pops u.p
Here is the code for actionperformed on the first jComboBox
first jComboBox actionperformed*
if(jComboBox13.getSelectedItem().toString().equals("SELECT")){
}else{
try{
String like = jComboBox13.getSelectedItem().toString();
String sql = "Select * From Room_Master\n" +
"inner join Room_Type on Room_Master.Room_Type_ID=Room_Type.Room_Type_ID\n" +
"where Room_Type = '"+like+"'";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
jComboBox14.removeAllItems();
jComboBox14.addItem("SELECT");
while(rs.next()){
String add1 = rs.getString("Room_No.");
jComboBox14.addItem(add1);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally {
try {
rs.close();
pst.close();
}catch(Exception e){
}
}
}
second jComboBox actionperformed
if(jComboBox14.getSelectedItem().toString().equals("SELECT") | jComboBox14.getSelectedItem().toString().isEmpty()){
}else{
try{
String like = jComboBox14.getSelectedItem().toString();
String sql = "Select * from Bed_Master\n" +
"inner join Room_Master on Bed_Master.Room_ID=Room_Master.Room_ID\n" +
"where [Room_No.] = '"+like+"'";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
jComboBox15.removeAllItems();
jComboBox15.addItem("SELECT");
while(rs.next()){
String add1 = rs.getString("Bed_No.");
jComboBox15.addItem(add1);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
e.printStackTrace();
}finally {
try {
rs.close();
pst.close();
}catch(Exception e){
}
}
}
but after i select another Room type it will work
i tried to remove "combobox.removeAllItems();"
but it will keep addding all the items in the jCombobox
almost 1 week trying to figure it out can someone help please
When you call removeAllItems it fires the actionListener for jComoboBox14
and at this stage it will not have any Items so getSelected will return NULL
change your if to
if(jComboBox14.getItemCount() > 0 && (jComboBox14.getSelectedItem().toString().equals("SELECT") |
jComboBox14.getSelectedItem().toString().isEmpty())){
First of all. You should give your objects variables an useful name:
Ex: jComboBox13 --> JComboBox comboRoomsType = new JComboBox(); or whatever name you prefer.
Then, it would be nice to see all the code involved. I can't see where you initialize the ResultSet or PreparedStatement;
I GUESS the NullPointer comes from the select statement, when trying to retrieve the values.
if(jComboBox13.getSelectedItem().toString().equals("SELECT")){
}else{
try{
String like = jComboBox13.getSelectedItem().toString();
String sql = "Select * From Room_Master RM " +
"inner join Room_Type RT on RM.Room_Type_ID=RT.Room_Type_ID "
+"where Room_Type = '"+like+"'";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
jComboBox14.removeAllItems();
jComboBox14.addItem("SELECT");
//you can also check if there are values first.
while(rs.hasNext()){
rs.next();
String add1 = rs.getString("Room_No");
//You can also use
//String add1 = rs.getInt(number of column of <Room_No> );
jComboBox14.addItem(add1);
}
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}finally {
try {
rs.close();
pst.close();
}catch(Exception e){
}
}
}

Java-Output all the columns and rows of a ResultSet

ta.setText is a TextArea where I want to show all my data from the database, after a button click. But with rs.get("name") I just output one value and it is always the last. How can I print out the whole table from the database, so all the information which are stored there?
try { String newquery = "SELECT * FROM kunden";
java.sql.PreparedStatement ps = con.prepareStatement(newquery);
rs = ps.executeQuery(newquery);
while (rs.next()){
ta.setText(rs.getString("name"));
ta.setText(rs.getString("nachname"));
}
}// try
catch(Exception e1) {
JOptionPane.showMessageDialog(null, "fail");
}
}//actionperformed
Either you build a string an then set that string using setText()
StringBuilder builder = new StringBuilder();
while (rs.next()) {
builder.append(rs.getString(“name”));
builder.append(“ “);
builder.append(rs.getString(“nachname”));
builder.append(“\n“);
}
ta.setText(builder.toString());
Or you use the append method that exists for TextArea
while (rs.next()) {
ta.append(rs.getString(“name”));
ta.append(“ “);
ta.append(rs.getString(“nachname”));
ta.append(“\n“);
}

print all the rows of of table into TextArea in javabook database

In my GUI, there is "Search" menuitem, and it should search the ssn number in the Faculty table and print them into the TextArea in javabook database. However, I only get the last row of the table that has 15 rows.
Here is the Search menuItem that calls the searchDB method.
MenuItem search = new MenuItem("Search");
search.setOnAction((ActionEvent t) -> {
try{
searchDB(tArea);
}
catch(Exception ex){
ex.printStackTrace();
}
});
Here is the searchDB method.
public void searchDB(TextArea tArea) throws SQLException, ClassNotFoundException {
if (databaseName != null) {
TextInputDialog dialog = new TextInputDialog();
dialog.setContentText("Please enter Faculty ssn number follwing by %");
Optional<String> search_id = dialog.showAndWait();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/" +databaseName+ "?autoReconnect=true&useSSL=false", "scott", "tiger");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from Faculty where ssn like '" + search_id.get() + "';");
while (resultSet.next())
tArea.setText(resultSet.getString(1) + "\t" + resultSet.getString(2) + "\t" + resultSet.getString(4) + "\t" + resultSet.getString(5));
}catch(Exception ex){
ex.printStackTrace();
}
}
}
Can you explain why do I get only the last row of the table? and How to print all the rows of the table in TextArea?
It is because you are using setText(), use append() instead. Check the javadoc for textarea.

Java sql text from database

i have a label and i want to upload with text from database, i wrote a method but its show just the first item from database, i want to see all item in column
try {
String sql="SELECT * FROM Arlista";
PreparedStatement pst=conn.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
while(rs.next()) {
arlab1.setText(rs.getString("nev"));
}
}catch(Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
while(rs.next()) {
arlab1.setText(rs.getString("nev"));
}
...overwrites the content of arlab each iteration. What you probably want to do is some kind of append to the result for each row;
String result = "";
while(rs.next()) {
if(result.isEmpty()) result = rs.getString("nev");
else result = result + "/" + rs.getString("nev");
}
arlab1.setText(result);

Categories