coding: in NETBEANS
DefaultTableModel model=(DefaultTableModel)depTb1.getModel();
try{
Class.forName("java.sql.Driver");
Connection con=DriverManager.getConnection("jdbcmysql://localhost/database","root","");
Statement smt=con.createStatement();
String q="select*from Departent";
ResultSet rs=smt.executeQuery(q);
while(rs.next())
{
int dno=rs.getInt("Deptno");
String dname=rs.getString("Dname");
String loc=rs.getString("Location");
model.addRow(new Object[] {dno,dname,loc});
}
rs.close();
smt.close();
con.close();
}catch (Exception e)
{
JOptionPane.showMessageDialog(null,"error in connectivity");
}
CODING IN SQL:
1. CREATE DATABASE DEMO;
2. USE DEMO;
3. CREATE TABLE DEPARTMENT (DEPTID INT(10),DNAME VARCHAR(10),LOCATION VARCHAR(10));
4.INSERT INTO DEPARTMENT VALUES (10,'RAVI','NOIDA');
No comments:
Post a Comment