This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problems with SQL

 
Mohd Zaki Mohd Salikon
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
problems with SQL sent to jdbc dbc ata: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

This problem is because of the program reads 1st tuple that was declared as a text in the database.
I was change the original databse. The oiginal database declared the 1st tuple as a number. So, how to solve it.
Thanks...
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 1st tuple means 1st row. So i am worried that the whole row is number or just the one column. And if just one column then it means that column would be number for all the tuples.

Anyways it is definitely not the cause of your exception as you are saying that it is on the connection string.
 
Mohd Zaki Mohd Salikon
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Opss...I'm very sorry. It's 1st column. The original data was declared as a number but I changed as a text.

The original data:
Matrix
12345
45211

The currently:
Matrix
AI123
AC254
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On which line the exception occurs. Need some code snippet.
 
Mohd Zaki Mohd Salikon
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problems is in the databases have a different types. The original database is declared as an integer but I was change as a text.

The declaration of Matrik:
Vector matrik = new Vector(9000,1);

The source code:


try {

String query = "SELECT Distinct Matrik FROM AmbilKursus ";
ResultSet rs = stmt.executeQuery(query);
String dat;

while (rs.next()) {
dat = rs.getString("Matrik");
if (dat != null)
matrik.addElement(dat);
}
int n=matrik.size();
subjek = new Vector[n];
System.out.println("Membaca subjek " +n+ " pelajar....");
for (int p=0; p<matrik.size(); p++) {
query = "SELECT Kursus FROM AmbilKursus Where Matrik = " + matrik.elementAt(p);
rs = stmt.executeQuery(query);
System.out.println((p+1));
subjek[p] = new Vector(1,1);
while (rs.next()) {
subjek[p].addElement(rs.getString("Kursus"));
}
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can still only guess given the error, but this lineis a problem. You must surround your strings with 's in SQL. In this case, you're better off with a PreparedStatement since it will escape any 's appearing in your string for you.

However, the quick solution is to change the above line toThis will work as long as Matrik is as you show above (alphanumeric). Never under any circumstances do this with user input. It's a major security hole but also can simply cause bugs.

My dinner's getting cold, so I'll leave the PreparedStatement solution as an exercise for you. Check the JavaDocs -- they're not that different from Statements, you just put ? as parameter placeholders in your query and use setString(x, str) to change the value.
 
Mohd Zaki Mohd Salikon
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David Harkness. U gave the solutions of that problems.
 
Mohd Zaki Mohd Salikon
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got a new problems. When I run my programs, there are smoothly build. On the way to get the output, programs become stuck. This message was appear:

java.lang.OutOfMemoryError
<<no stack trace available>>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic