Good Morning Sir,
Please help me, I make a small code but i have a error. I want to make viewer photo from database MySql into my project in netbeans. this is my code :
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:hyt","root","");
Statement stat = con.createStatement();
ResultSet res = stat.executeQuery("select * from kar where nik ='"+jTextField1.getText()+"'");
if (res.next())
{
jTextField1.setText(res.getString("nik"));
jTextField9.setText(res.getString("nm_kary"));
jComboBox6.setSelectedItem(res.getString("jns_klmin"));
jComboBox2.setSelectedItem(res.getString("jabatan"));
jTextField3.setText(res.getString("cab"));
jComboBox7.setSelectedItem(res.getString("pend"));
jTextField20.setText(res.getString("nm_scul"));
jDateChooser2.setDate(res.getDate("tgl_msk"));
eGambar.setText(foto(jTextField1.getText()));
Blob blob2 = res.getBlob("poto");
jLabel24.setIcon(new javax.swing.ImageIcon(blob2.getBytes(1, (int) (blob2.length()))));
//
jPanel9.setVisible(true);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"error "+e);
}
i have error : error java.lang.UnsupportedOpertionException
please help me, Thank you, regards, Hendra
The given code accept the id from the user and display the image, name and address corresponding to the id.
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class RetrieveImageWithData{
public static void main(String[] args)throws Exception{
final JFrame f=new JFrame();
f.setLayout(null);
final JLabel lab=new JLabel("Enter ID: ");
final JTextField text=new JTextField(20);
final JButton b=new JButton("Display");
final JLabel lab1=new JLabel();
final JLabel lab2=new JLabel("Name");
final JTextField text1=new JTextField(20);
final JLabel lab3=new JLabel("Address");
final JTextField text2=new JTextField(20);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int id=Integer.parseInt(text.getText());
lab1.setVisible(true);
lab2.setVisible(true);
text1.setVisible(true);
lab3.setVisible(true);
text2.setVisible(true);
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root" );
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select * from data where id="+id+"");
byte[] bytes = null;
if(rs.next()){
String name=rs.getString("name");
text1.setText(name);
String address=rs.getString("address");
text2.setText(address);
bytes = rs.getBytes("image");
Image image = f.getToolkit().createImage(bytes);
ImageIcon icon=new ImageIcon(image);
lab1.setIcon(icon);
}
}
catch(Exception ex){
System.out.println(ex);
}
}
});
lab.setBounds(10,10,100,20);
text.setBounds(150,10,100,20);
b.setBounds(250,10,100,20);
lab1.setBounds(150,40,100,70);
lab2.setBounds(10,120,100,20);
text1.setBounds(150,120,100,20);
lab3.setBounds(10,150,100,20);
text2.setBounds(150,150,100,20);
lab1.setVisible(false);
lab2.setVisible(false);
text1.setVisible(false);
lab3.setVisible(false);
text2.setVisible(false);
f.add(lab);
f.add(text);
f.add(b);
f.add(lab1);
f.add(lab2);
f.add(text1);
f.add(lab3);
f.add(text2);
f.setVisible(true);
f.setSize(350,200);
}
}
Hello,
Thank you for your help. How to function this code, i already used that but cannot view anything. can you help me again. thank you
regards, hendra