JTable: display strings with newline characters as multiple lines - java

I added a JTable in a JFrame using drag and drop in netbeans.
I was able to display the contents of the database but it is not like what i want to display, i want strings with newlines to be display as multiple lines.
i have a code here that query in the sqlite database and then displayed it in the table.
public void Update_table_RFID(JTable x){
try{
String sql = " select RFID as 'RFID Tag', Word, ARF as 'Audio Recording File', "
+ " DateAdded as 'Date Added', TimeAdded as 'Time Added' "
+ " from RFID";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
x.setModel(DbUtils.resultSetToTableModel(rs));
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
}
but the output i got instead is this:
what do i need to do to display the correct output of the query in the table?

Related

Sqlite Update query data in database not updating java gui application

Guys i am trying to update data into my database I have already created insert and a delete option but after creating this update query my data is not being updating in database so i request for your help please help me my problem is in video and tutorial available online the data is updating using an where statement in which they use a integer datatype but my database don't have any integer field i have only string field and data is not being updated please help the code of update button is posted below enter code here
JButton ud_btn_Update = new JButton("Update");
ud_btn_Update.setFont(new Font("Tahoma", Font.BOLD, 20));
ud_btn_Update.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try { String query="update Customers set "
+ "Name='"+ud_tf_Name.getText()+"' "
+ ",Address='"+ud_tf_Address.getText()+"' "
+ ",Gstin='"+ud_tf_Gstin.getText()+"' "
+ ",Discount='"+ud_tf_Discount.getText()+"' "
+ ",State='"+ud_tf_State.getText()+"' "
+ ",StateCode='"+ud_tf_StateCode.getText()+"' "
+ " where Name='"+ud_tf_Name.getText()+"' ";
PreparedStatement pst = connection.prepareStatement(query);
pst.execute();
JOptionPane.showMessageDialog(null, "Data Updated!");
pst.close();
} catch(Exception e4) {
e4.printStackTrace();
}
Your code seems syntactically correct and it should work if the table exists and the table and column names are not misspelled.
What may cause the code to not update the row in the table is the where clause.
This value:
ud_tf_Name.getText()
is obviously the new name that you want to replace the old value in the column name, right?
So, it does not exist in the table and the where clause does not return any row and nothing is updated.
You must save the old name in a string variable, say oldname and use it in the where clause:
............................................
+ " where Name='"+oldname+"' ";
Also you must learn to use parameters with the prepared statements to create safer code.

How to show the whole column in a text area?

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");
}

2 SQL queries in one try/catch. Not working

Basically, I have to show a list with the data from a database table [that part is working] and afterwards I have to show the highest Date [a date variable in the table]. The second part is not working no matter what I do.
Here's the code
try {
String SQL = "SELECT * FROM tb_rafael";
ResultSet rs = BD.consultar(SQL);
String tab = "";
int numReg = 0;
while (rs.next()) {
tab+="<TR>";
tab+="<TD>" + rs.getString("nme_rafael") + "</TD>";
tab+="<TD>" + rs.getString("dta_rafael") + "</TD>";
tab+="</TR>";
numReg++;
//mDat = rs2.getString("dta_rafael");
}
rs.close();
dados.put("DADOS", tab);
dados.put("NUM_REG", String.valueOf(numReg));
//Pegar Data Maior
String SQL2 = "SELECT MAX(dta_rafael) FROM tb_rafael";
ResultSet rs2 = BD.consultar(SQL2);
String mDat = "";
//while(rs2.next()){
mDat = rs2.getString("dta_rafael");
//}
rs2.close();
dados.put("MDA", mDat);
} catch (Exception ex) {
dados.put("MSG", "Erro: " + ex.getMessage());
}
What you want to look at is past the commentary line "Pegar Data Maior". That's the part that is not working. I've tried adding a while, using a different ResultSet, using the same ResultSet and none of those worked. I know it's not an issue with the SQL query since I tested it with the workbench and it returned me the data I want.
To be more specific, I don't get an error message or anything, the dados.put simply does not work and I get just this:
How the HTML code looks:
The data should show up where the {MDA} is. Anyone have any ideas?
The query SELECT MAX(dta_rafael) FROM tb_rafael may not return a column name, which you later try to retrieve, rs2.getString("dta_rafael");
I'd change the query to SELECT MAX(dta_rafael) AS Max_date..., and reference to MAX_date thereafter.

SQL Update statement to update document file and photo - varbinary (max)

I wrote a SQL UPDATE statement to update document file and photo stores as varbinary(max) in the DB. At first I got an error saying
Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query
So I changed the code and did it like this.
try {
//SET Varbinary_Col = CAST(Varbinary_Content as VARBINARY)
query = "UPDATE tourClient SET DocumentFile= CONVERT(varbinary(max),'"+docFile+"'),Photo= CONVERT(varbinary(max),'"+person_image+"') WHERE Name= '" + combo_client.getSelectedItem() + "' ";
PreparedStatement stm = db.getconn().prepareStatement(query);
int val = stm.executeUpdate();
if (val > 0) {
JOptionPane.showMessageDialog(null, "Uploaded successfully!!!");
rs.next();
}
db.getconn().close();
} catch (Exception e) {
System.out.println(e);
}
After I run the above query in netbeans, i get the Joption message saying uploaded successfully. I have another jframe in netbeans, which has a query that SELECTS all document files, Images and shows it in a jtable. When I click a row in jtable the image is shown in a label and the document is opened but when I try to open it, it says the file is corrupted and for the image it doesnt show the png that I uploaded.
Have I written the statement correctly? Anything I have to change?

Jtable selected row in netbeans that is bounded to sql server

I fill a jtable(tbl_student) in netbeans by this code:
String[][] result;
result = stu.Search(txt_search.getText());
String hdr[] = {"code", "name", "family"};
tbl_student = new JTable(result, hdr);
jScrollPane1.setViewportView(tbl_student);
tbl_student.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
That Search() function is a select all query from a table in sql server database.
Now, I want to find selected row in this table. How can I do it?
My code in mouse click event of this jtable is not work!
What can I do?
If you need to get some data when you clicked a row in a JTable use the following code.
int row=tbl_student.getSelectedRow();
String Table_data=(tbl_student.getModel().getValueAt(row, 0).toString()); // In here 0 means the column number.
//If you have a JTable with 5 columns; 0 is the 1st column and 4 is the last (5th) column.
If you want to get data in a database table to a JTable, their is a library called rs2xml.jar with the help of that library you can simply fill a JTable with database data.
You can download that library from HERE.
After downloading library use the following code to fill your JTable.
Connection conn=null;
ResultSet rs=null;
PreparedStatement pst=null;
try{
String sql="SELECT * FROM table_name"
conn=java_connect.ConnecrDb(); //Database connecting class
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
tbl_student.setModel(DbUtils.resultSetToTableModel(rs));
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
finally{
try{
rs.close();
pst.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}

Categories