All pastes #1776722 Raw Copy code Copy link Edit

Someone

public unlisted java v1 · immutable
#1776722 ·published 2010-02-03 06:08 UTC
rendered paste body
import java.io.*;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.sql.*;import java.util.*;import ca.odell.glazedlists.BasicEventList;import ca.odell.glazedlists.EventList;public class DbDriver {    public static void main(String[] args)throws IOException    {        try         {	        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");	        // set this to a MS Access DB you have on your machine	        //String path = "C:/Documents and Settings/student224.PAULVI/Zac Gorak/My Dropbox/Java/databases/";	        String path = "C:/Users/Zac/Documents/My Dropbox/Java/databases/";	        	        String dbname = "options3.0.mdb";	        String source = path + dbname;	        String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";	        database+= source.trim() + ";DriverID=22;READONLY=true}"; // add on to the end 	        // now we can get the connection from the DriverManager	        Connection con = DriverManager.getConnection( database ,"",""); 	        	        Statement s = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 	        String sqlPrint = "SELECT * FROM [Peer Mentors Info]";	        s.execute(sqlPrint);	        	        ResultSet rs = s.getResultSet();	    	ResultSetMetaData rsmd = rs.getMetaData();	        int cols = rsmd.getColumnCount();        		                    String imports = "";            String head =             	"public class Student \n" +            	"{\n";            String close = "}";            String src = System.getProperty("user.dir");            String className = "Student";            String fileName = "/src/" + className + ".java";            File file = new File(src + fileName);                        if(!file.exists())            {	            Writer output = null;	            output = new BufferedWriter(new FileWriter(file));	            output.write(head);	        		        if (rs != null) // if rs == null, then there is no ResultSet to view		        {		        	output.write("\t//Instance Fields\n");		        	for( int x = 1; x-1 < cols; x++)		           	{		      			String name = rsmd.getColumnName(x);		      			String type = rsmd.getColumnClassName(x);		      					      			output.write ("\tprivate " + type + " " + name + ";\n");		        	}		        			        	output.write("\n\t//Modifiers\n");		        	for( int x = 1; x-1 < cols; x++)		           	{		      			String name = rsmd.getColumnName(x);		      			String type = rsmd.getColumnClassName(x);		      					      			output.write("\t/**\n\t* @param " + name + "\n\t*/\n");		      					      			output.write ("\tpublic " + "void set" + name + "( " + type + " " + name +" )\n");		      			output.write("\t{\n\t\tthis." + name+ " = " + name + ";\n\t}\n");		        	}		        			        	output.write("\n\t//Accessors\n");		        	for( int x = 1; x-1 < cols; x++)		           	{		      			String name = rsmd.getColumnName(x);		      			String type = rsmd.getColumnClassName(x);		      					      			output.write("\t/**\n\t* @return this." + name + "\n\t*/\n");		      					      			output.write ("\tpublic " + type + " get" + name + "()\n");		      			output.write("\t{\n\t\treturn " + name + ";\n\t}\n");		        	}		        	       			        	output.write("\n\t/**\n\t* Class constructor - Default - No instance fields\n\t*/\n");		        	output.write("\tpublic " + className + "()\n\t{\n\t\t");		        	for( int x = 1; x-1 < cols; x++)		           	{		      			String name = rsmd.getColumnName(x);		      			String type = rsmd.getColumnClassName(x);		      					      			output.write ("this." + name + " = null; ");		        	}		        	output.write("\n\t}\n");		        			        	output.write("\n\t/**\n\t* Class constructor - Super! - Every instance field\n\t*/\n");		        	output.write("\tpublic " + className + "( ");		        	for( int x = 1; x-1 < cols; x++)		           	{		      			String name = rsmd.getColumnName(x);		      			String type = rsmd.getColumnClassName(x);		      					      			if(x < cols)		      				output.write (type + " " + name + ", ");		      			else		      				output.write (type + " " + name);		        	}		        	output.write(" )\n\t{\n\t\t");		        	for( int x = 1; x-1 < cols; x++)		           	{		      			String name = rsmd.getColumnName(x);		      			String type = rsmd.getColumnClassName(x);		      					      			output.write ("this." + name + " = " + name + "; ");		        	}		        	output.write("\n\t}");	        	}		        output.write("\n" + close);		        output.close();		        System.out.println("Your file, " + fileName + ", has been written");            }            else             {            	System.out.println("Your file, " + fileName + ", already exists! Force write if this is a mistake.");            }	        	        	        Student noInstanceStudent = new Student();	        System.out.println("Sample Student, " + noInstanceStudent + ", created succesfully!");	        	        EventList idols = new BasicEventList();	        	        while ( rs.next())	        {	        	Student instance = new Student();	    		for( int x = 1; x-1 < cols; x++)	    		{ 	    			String method = "set"+ rsmd.getColumnName(x);	    			String arg = rs.getString(x);	    			String crsmd = rsmd.getColumnClassName(x);	    			//System.out.println(crsmd);	    			Class cn = Class.forName(rsmd.getColumnClassName(x));	    			Class[] crar = { cn };	    			Object[] arguments = new Object[1];	    				    			//There has to be a better way!	    			if(cn.toString().contains("Integer"))	    			{	    				arguments[0] = Integer.parseInt(arg); 	    			}	    			else if(cn.toString().contains("String"))	    			{	    				arguments[0] = arg; 	    			}	    			else if(cn.toString().contains("Boolean"))	    			{	    				arguments[0] = Boolean.valueOf(arg); 	    			}	    			else if(cn.toString().contains("Timestamp"))	    			{	    				arguments[0] = Timestamp.valueOf(arg); 	    			}	    			Object o = null;	    			try {	    			      Class<?> c = instance.getClass();		  	    		Method m = c.getDeclaredMethod(method, crar);		  	    			    			    //System.out.println(m);	    			      // m.setAccessible(true); // solution	    			    o = m.invoke(instance, arguments); // IllegalAccessException		    				  	    				    			      // production code should handle these exceptions more gracefully	    			    } catch (NoSuchMethodException z) {	    			      z.printStackTrace();	    			    } catch (InvocationTargetException z) {	    			      z.printStackTrace();	    			    } catch (IllegalAccessException z) {	    			      z.printStackTrace();	    			    }	    		}	    		idols.add(instance);	        }	        System.out.println("EventList, \n" + idols + "\n, of students created");        	        	        rs.close();	        s.close(); // close the Statement to let the database know we're done with it            con.close(); // close the Connection to let the database know we're done with it        } catch (Exception e) {            System.out.print("Error: "); e.printStackTrace();        }    }}