Create and View the Database Table in Java Swing


Download the database and jar file here download

      1.Create Java Project in Netbeans

      2.Create JFrame form

     3.Drag & Drop JTable


    4.Delete the predefined columns




   5.Then the jtable look like this


   6.Insert this getTable() method in your class 

public JTable getTable(int value)throws Exception{
String query=null;
query="select * from jsample where id='"+value+"'";
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/path","root","root");
Statement s1=con.createStatement();
DefaultTableModel dm=new DefaultTableModel();
ResultSet rs=s1.executeQuery(query );
ResultSetMetaData rsmd=rs.getMetaData();
//Coding to get columns-
int cols=rsmd.getColumnCount();
String c[]=new String[cols];
for(int i=0;i<cols;i++){
c[i]=rsmd.getColumnName(i+1);
dm.addColumn(c[i]);
}
//get data from rows
Object row[]=new Object[cols];
while(rs.next()){
for(int i=0;i<cols;i++){
row[i]=rs.getString(i+1);
}
dm.addRow(row);
}
jTable1.setModel(dm);     //change your table name here
con.close();
return jTable1;                 //change your table name here
  }

   7.Call your method from your Action 

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
getTable(1);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}

  8.Output






Autocomplete using jquery and Mysql Database



Requirement:

Required javascript and css file Download Here

Required jsp files see below code

update.jsp

<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script src="jquery.autocomplete.js"></script>
<script language="javascript" type="text/javascript">

jQuery(function(){
$("#country").autocomplete("reg_update.jsp");
});
</script>
<title>Ganesh Rengarajan</title>
</head>
<body onLoad="show_clock()">
<table align="center" border="0" width=" 60%" cellspacing="6">
<tr>
<td >Select Name</td>
<td><input name="appno" id="country" name="country" size="20" ></td>
</tr>
</table>
</body>
</html>


reg_update.jsp

<%@page import="java.sql.*"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%

try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/path","root","root");
Statement s1=con.createStatement();
Statement st1=con.createStatement();
ResultSet rs=st1.executeQuery("select * from jsample");
String product="";
ArrayList aa=new ArrayList();
while(rs.next())
{
aa.add(rs.getString("name"));
}
int cnt=1;
String query = (String)request.getParameter("q");

for(int i=0;i<aa.size();i++)
{
String temp=(String) aa.get(i);

if(temp.toUpperCase().startsWith(query.toUpperCase()))
{
out.print(temp+"\n");
if(cnt>=10)
break;
cnt++;
}
}

}
catch(Exception ex)
{
System.out.println("error"+ex);
}

%>

 
java4practices © 2013 | Designed by Ganesh Rengarajan