I entered the code and I wanted to send this data to the prepared table in Workbench (SQL). The system shows no connection. I work in an eclipse environment and I have problems to send data from jtable to Workbench
JButton btnUpdate = new JButton("Update");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/tabela2?autoReconnect=true&useSSL=false","root","Jackpatu205");
String insert = "insert into autohandel values (?, ? , ? , ?,?)";
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(insert);
ps.setString(1, "1");
ps.setString(2, "Citroen");
ps.setString(3, "2015");
ps.setString(4, "12500");
ps.setString(5,"France");
ps.executeUpdate();
if (conn !=null) {
System.out.println("\nConnected Update");
System.out.println(" --------------------------------------");
}
}catch (Exception e) {
System.out.print("No Connection Update");
}
//
}
});
Related
I'm making a small application for a school project. Inserting photos into SQL database is fine, I rename the photos in a JTable and can also open blob photos in SQL editor. It is not possible to make the photos visible on a JLabel.
Can someone help me on this?
The intention is to get a preview of the information that has been entered at a mouse click. the application is an image bank where you can enter different healthcare devices.
Code JTable
DefaultTableModel model = (DefaultTableModel) tableDom.getModel();
try {
Class.forName("com.mysql.jdbc.Driver"); //database connectie maken
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybrains?
allowMultiQueries=true&serverTimezone=America/Winnipeg&dummyparam=", "", "")) {
String sql = "Select * from Telefonie ";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery(sql);
model.setRowCount(0);
while (rs.next()) {
int id = rs.getInt("ID");
String naam = rs.getString("naam"); // naam is een kollom in de database
String model_type = rs.getString("Model_Type");
String functionaliteit = rs.getString("Functionaliteit");
String troubleshoot = rs.getString("Troubleshoot");
String oplossing = rs.getString("Oplossing");
String voorbeeld = rs.getString("Fotonaam");
byte[] fotos = rs.getBytes("Uploadfoto");
// ImageIcon imageicon = new ImageIcon(fotos);
//voorbeeldfoto.setIcon(imageicon);
//voorbeeldfoto.setIcon(foto());
model.addRow(new Object[]{id, naam, model_type, functionaliteit, troubleshoot,
oplossing, voorbeeld, fotos});
}
}
} catch (ClassNotFoundException | SQLException e) {
}
Code Mouse Clicked
try {
int i = tableDom.getSelectedRow();
Class.forName("com.mysql.jdbc.Driver"); //database connectie maken
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybrains?
allowMultiQueries=true&serverTimezone=America/Winnipeg&dummyparam=", "", "");
String sql = "Select * from Telefonie";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery(sql);
if (rs.next());
//int i = tableDom.getSelectedRow();
TableModel model = tableDom.getModel();
iD.setText(model.getValueAt(i, 0).toString());
name.setText(model.getValueAt(i, 1).toString());
modelt.setText(model.getValueAt(i, 2).toString());
func.setText(model.getValueAt(i, 3).toString());
oplos.setText(model.getValueAt(i, 4).toString());
troubl.setText(model.getValueAt(i, 5).toString());
photoNaam.setText(model.getValueAt(i, 6).toString());
byte[] voorbeeldfoto = rs.getBytes("Uploadfoto");
JLabel image1 = (JLabel) model.getValueAt(i, 8);
ImageIcon image1Icon = (ImageIcon) image1.getIcon();
voorbeeldfoto.setIcon(image1Icon);
} catch (ClassNotFoundException | SQLException ex) {
JOptionPane.showMessageDialog(rootPane, name);
}
}
I have a problem with my code and it has no error log so I can't see the problem . . .
Current Problem:
Mainclass() is where the main frame is. . . Clicking ADD will open
AddBroker() and clicking ADD in AddBroker() will update databse and jtable
in MainClass(). Database can now be updated but the jtable in MainClass() won't
change until you open it again
This is my main class (other codes were redacted to focus on problem)
public class MainClass extends JFrame {
JButton button_17 = new JButton("ADD");
button_17.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//to call class AddBroker()
AddBroker ab = new AddBroker();
ab.setVisible(true);
}
});}
Then this is the class for AddBroker(). . .
public class AddBroker extends JFrame {
JButton btnAdd = new JButton("ADD");
final Object[] addBrokerrow = new Object[3];
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ButtonCon bcd = new ButtonCon();
DriverTableCon dtcd = new DriverTableCon(); //this is just jtable
MainClass mcd = new MainClass();
String a = brokerBroker.getText();
String b = addBroker.getText();
String c = tinBroker.getText();
addBrokerrow[0] = a;
addBrokerrow[1] = b;
addBrokerrow[2] = c;
dtcd.modelBroker.addRow(addBrokerrow);
bcd.addBrokerCon(a,b,c);
}
});}
And ButtonCon() is where the adding of data is
public class ButtonCon {
Connection con;
Statement st;
ResultSet rs;
StringBuffer results;
String url = "jdbc:ucanaccess://C://DATABASE//NTD.accdb";
public void addBrokerCon(String broker, String add, String tin) {
try {
con = DriverManager.getConnection(url);
String sql = "INSERT INTO brokerT (Broker, Address, Tin_No) VALUES (?,?,?)";
ps = con.prepareStatement(sql);
ps.setString(1, broker);
ps.setString(2, add);
ps.setString(3, tin);
ps.executeUpdate();
ps.close();
con.close();
}
catch (Exception e) {
System.out.print(e.toString());
}
} }
There is no error so I have no idea what is wrong here. Any input would be appreciated :)
So it's been a week and what I did try to solve the problem and what I did is add a "Refresh" button that loads the table again.
You fogot about ps.executeUpdate(); between ps.setString(3, tin); and ps.close();.
And remove all with st it is not necessary.
How would i get the "Goal" value from my MySQL into a JLabel? Would i have to separate it into different functions?
Jlabel GOAL = new JLabel("");
try {
//connection to database
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:8889/Daily", "student", "student");
Statement myStmt = myConn.createStatement();
ResultSet rs = myStmt.executeQuery("select Goal from User WHERE id=1");
GOAL.setText(rs.getString("Goal"));
}
catch (Exception e) {
e.printStackTrace();
}
GOAL.setForeground(Color.LIGHT_GREEN);
GOAL.setBounds(23, 90, 61, 45);
contentPane.add(GOAL);
`
I'm trying to display a list of data when I click on a button.
Now, I have a an homepage (a jframe that is the homepage of the program) with a button that will open the list frame and that button, when it will be clicked, will get the data from my DB and update the table.
This is the code that I've wrote on the button that is in the homepage and not in the list frame
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
try {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new List().setVisible(true);
}
});
this.dispose();
String DB_URL = "jdbc:mysql://localhost:3306/registropassword?autoReconnect=true&useSSL=false";
String DB_Username = "root";
String DB_Password = "root";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL, DB_Username, DB_Password);
PreparedStatement prepstmt = null;
ResultSet rs = null;
String query = "SELECT * FROM registro";
prepstmt = conn.prepareStatement(query);
rs = prepstmt.executeQuery();
jTable2.setModel(DbUtils.resultSetToTableModel(rs));
} catch (ClassNotFoundException ex) {
Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
}
}
NOTE:
On this line of code the jTable2, that is the name of the table that should be updated, I got a 'cannot find symbol' error on this line.
jTable2.setModel(DbUtils.resultSetToTableModel(rs));
Can someone help me to figure out where I'm wrong?
I have two MySQL tables as image below
movie
movie_title
JComboBox combo = new JComboBox();
combo.setBounds(125, 15, 190, 20);
try {
DatabaseConnection db=new DatabaseConnection();
Connection connect=db.getConnection();
String sql="Select title FROM movie_title";
PreparedStatement ps=connect.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String name = rs.getString("title");
combo.addItem(name);
}
} catch (Exception e) {
System.out.println("null");
}
combo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JComboBox comboBox=(JComboBox) event.getSource();
Object selected = comboBox.getSelectedItem();
displayDay(selected);
}
private void displayDay(Object selected) {
// TODO Auto-generated method stub
try {
combo1.removeAllItems();
DatabaseConnection db=new DatabaseConnection();
Connection connect=db.getConnection();
String sql="Select movie_day FROM movie WHERE movie_title = ?";
PreparedStatement ps=connect.prepareStatement(sql);
ps.setObject(1, selected);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String day = rs.getString("movie_day");
combo1.addItem(day);
}
} catch (Exception e) {
System.out.println("null");
}
}
});
I have implemented ActionListener in comboBox. When user select movie Marvel's Captain America, it will get the movie_day item from movie and load into combo1. Is there a way I can make the combo1 display the movie_day which is Sunday, 28 Apr 2016 one time only instead of two ?
Edit
private void displayDay(Object selected) {
// TODO Auto-generated method stub
try {
combo1.removeAllItems();
DatabaseConnection db=new DatabaseConnection();
Connection connect=db.getConnection();
String sql="Select movie_day FROM movie WHERE movie_title = ?";
PreparedStatement ps=connect.prepareStatement(sql);
ps.setObject(1, selected);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String day = rs.getString("movie_day");
DefaultComboBoxModel model = (DefaultComboBoxModel)combo1.getModel();
if (model.getIndexOf(day) == -1)
{
combo1.addItem(day);
}
}
} catch (Exception e) {
System.out.println("null");
}
Is there a way I can make the combo1 display the movie_day which is Sunday, 28 Apr 2016 one time only instead of two ?
Before adding the date to the combo box you need to check if the date already exists.
DefaultComboBoxModel model = (DefaultComboBoxModel)comboBox.getModel();
if (model.getIndexOf(theDate) == -1)
{
comboBox.addItem( theDate );
}
You could also change the SQL statement to only get "unique" dates, but I don't know SQL well enough to give you the actual syntax. Maybe this SQL Tutorial will help, otherwise you need to find a better tutorial.