PDA

View Full Version : Java and SQL printing values separately


tefybaez
11-28-2008, 12:01 PM
I have an access database connected to a java program i wrote. Its supposed to be used to make a school schedule without allowing any hour collision or classroom etc. I have a table where the schedules are saved, in that table there's a colum in which i save the classroom, hours and day of the class
This is the format in which its saved (CLASSROOM-STARTS-ENDS-DAY)
Is there a way i can then print those separately ? If i wanted to show each one in separate columns using java, is that possible ? If so, how ??

Thanks A LOOOOT in advance.

Paul Komski
11-28-2008, 02:47 PM
I presume you mean that you have a java program that you wrote (the front end) which accesses an MSAccess database (the back end) where the data for the front end is stored.

Let us know if I have misunderstood.

If however that is the case is all the data stored in Access Tables and how is the database accessed; by ODBC or some other method?

Also could you expand on the SQL part. Are these SQL queries run from java and if so can you copy the full syntax of the SQL statements here.

I may be able to help to a certain extent but have never programmed, in earnest, in Java.

tefybaez
11-30-2008, 11:07 AM
heres the method i used to access the database

Driver d = (Driver)Class.forName
("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection c = DriverManager.getConnection(
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=horariosunibe.mdb"
);


And you're right.
I run the SQL queries from java

heres the query:
sql = "SELECT * FROM HORARIO";
rs = stmt.executeQuery(sql);

I managed to do what I wanted to do using this:

String temp = rs.getString("LOCALIZACION_HORA");
String [] temp2 = temp.split("\\-");

Now my problem supposedly the table is a mess so i have to re-do it...
Are you a database designer? or do you know anything about this? Maybe you could help with that =)


Thanks anyways!

Paul Komski
12-02-2008, 03:24 AM
The java end of things is too murky in the dim distant past for me to be able to help there. I have programmed fairly extensively with both MSAccess and PHP/SQL but from what you have posted it appears that you can run the query successfully and I can only guess that the variable rs is a simple recordset of the whole table.

How that variable/recordset is utilised in java is foreign to me. The two "temp/temp2" strings have syntax that I don't understand; viz:-

String temp = rs.getString("LOCALIZACION_HORA");
String [] temp2 = temp.split("\\-");

Nor do I know what LOCALIZACTION_HORA refers to.

In MSAccess, having first created a string variable and a recordset variable to be accessed by the program (say the temp and rs variables as used by you) one would typically then access the recordset by looping through it from first to last row or EOF (EndOfFile) and using some criteria to use the values for the fields (as represented by the column names) in the row.

For example it the Database being accessed was called MyDb then the code in MSAccess might go along the lines of:

sql = "SELECT * FROM HORARIO";
Set rs = MyDb.OpenRecordset(sql)

rs.MoveFirst
Do Until rs.EOF
If rs("CLASSROOM") = 7 Then
Debug.Print rs("DAY") " lasts for " rs("ENDS") - rs("STARTS") & " hours"
End If
rs.MoveNext
Loop

If you have nothing equivalent in Java then I can't really help any further.