We help IT Professionals succeed at work.

How to remove the query effect in jdbc, how to close a window in swings

searchsanjaysharma
on
I have Main.java file.It contains Search option to searh by rollno.If i search by rollno then class viewr.java is called, but after which when i click on search by name, it doesnt fetch the result as it takes the previous query.
I am attaching all file.Compile all those individually and run Main.java.
Also if i call a class called as viewall.java viewr.java viewname.java it exits the program on close button, how to return to Main.java
   calladd.java calladd.java
calldel.java
calldelete.java
calledit.java
callsave.java
callupdate.java
Main.java
student.mdb
viewall.java
viewname.java
viewr.java
Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
BUt you cannot hav eclass QueryTableModel with the same name thrre times in one oprokect - files viewall, viewr, and viewname
Awarded 2011
Awarded 2011

Commented:
Especially you want them to be three different classes - three classes with the same name cannot coexist in one project, even if they are in different .java files
Awarded 2011
Awarded 2011
Commented:
and in general you woull not ned to have three of them if they differe say only by query.
Youi should have one class - and its constructor should take a string that would be the actual querry
this is soemthing which would be more or less normal
Redesign the application in this way - and I'm sure your issues which you mentioned above will change

You probably are not using  IDE - otherwsie it will immediately compalin about duplicate cllasess
in one project

Author

Commented:
yup, thanks i had done all hardcoding.let me follow this approach.
Awarded 2011
Awarded 2011

Commented:
You should have only one class QueryTableModel - something like below -
I modified the constructor - it takes parameter the query string -
and in each of thease viewall, viewr, viewname you create instances of this one class
but with different query string parameter value

class QueryTableModel extends AbstractTableModel 
{
	Vector cache; // will hold String[] objects . . .
	int colCount;
	String[] headers;
	Connection db;
	Statement statement;
	String currentURL;
	Connection conn;
    String sqlString;
	
	public QueryTableModel(String s) 
	{
		cache = new Vector();
        sqlString = s;
	 
	}

	public String getColumnName(int i) 
	{
		return headers[i];
	}

	public int getColumnCount() 
	{
		return colCount;
	}
	public int getRowCount() 
	{
		return cache.size();
	}
	
	public Object getValueAt(int row, int col) 
	{
		return ((String[]) cache.elementAt(row))[col];
	}
	public void setHostURL(String url) 
	{
		if (url.equals(currentURL)) 
		{
		// same database, we can leave the current connection open 
		return;
		}
		// Oops . . . new connection required
		closeDB();
		initDB(url);
		currentURL = url;
	}

	// All the real work happens here; in a real application,
	// we'd probably perform the query in a separate thread.
	public void setQuery(String q) 
	{

		cache = new Vector();
		try 
		{
			//String s="select * from student where rno='"+q+"' order by sname";
			String s = sqlString;
            System.out.print(s);
						// Execute the query and store the result set and its metadata
			ResultSet rs = statement.executeQuery(s);
			ResultSetMetaData meta = rs.getMetaData();
			colCount = meta.getColumnCount();
			// Now we must rebuild the headers array with the new column names
			headers = new String[colCount];
			for (int h = 1; h <= colCount; h++) 
			{
				headers[h - 1] = meta.getColumnName(h);
			}
			// and file the cache with the records from our query. This would
			// not be
			// practical if we were expecting a few million records in response
			// to our
			// query, but we aren't, so we can do this.
			while (rs.next()) 
			{
				String[] record = new String[colCount];
				for (int i = 0; i < colCount; i++) 
				{
					record[i] = rs.getString(i + 1);
				}
				cache.addElement(record);
			}
			
			fireTableChanged(null); // notify everyone that we have a new table.
		}
		catch (Exception e) 
		{
			cache = new Vector(); // blank it out and keep going.
			e.printStackTrace();
		}
	}

	public void initDB(String url) 
	{
		try
		 {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			db = DriverManager.getConnection("jdbc:odbc:studsn","","");
			statement = db.createStatement();
		}
		catch (Exception e) 
		{
			System.out.println("Could not initialize the database.");
			e.printStackTrace();
		}
	}
	public void closeDB() 
	{
		try 
		{
			if (statement != null) 
			{
				statement.close();
				conn.close();
			}
			if (db != null) 
			{
				db.close();
			}
			}
			catch (Exception e) 
			{
				System.out.println("Could not close the current connection.");
				e.printStackTrace();
			}
		}
	}

Open in new window

Author

Commented:
Thanks a ton
Awarded 2011
Awarded 2011

Commented:
You are always welcome.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.