Hi I am trying to connect a database through Java. I have written a program and it throws exceptions.I am not so strong in java and database connections and I am a newbie. Please anyone help me as soon as possible as its urgent!!!
Here is my program:
import java.sql.Connection;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.SQLException;
class db{
ResultSet rs,rs1;
Connection con=null;
Statement stmt;
db()throws Exception{
con=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:test");
stmt=con.createStatement();
}
void store(String name,String password,String dateofreg)throws Exception{
boolean flag=true;
rs=stmt.executeQuery("select username from userinformation where username=name");
while(rs.next()&&flag==true)
{
String str=rs.getString("username");
if (str.equals(name))
flag=false;
}
if(flag!=false){
rs1=stmt.executeQuery("insert into userinformation(username,password,dateofreg) values(username,password,dateofreg)");
}
if(flag==false)
{System.out.println("Storage failed");}
}
void viewall()throws Exception
{
rs=stmt.executeQuery("select * from userinformation");
while(rs.next())
{
System.out.println(rs.getString("userid")+"\t"+rs.getString("username")+"\t"+rs.getString("password")+"\t"+rs.getString("dateofreg")+"\n");
}
}
boolean lookup(String name,String pswd)throws Exception{
rs=stmt.executeQuery("select password from userinformation where username=name");
String pswd1=rs.getString("password");
if(pswd.equals(pswd1)==true)
return true;
else
return false;
}
}
class database{
public static void main(String args[])throws Exception
{
db dbase=new db();
dbase.store("Ram","raju","30-9.-06");
dbase.store("Ram","raju","30-9.-06");
dbase.viewall();
dbase.lookup("Ram","raju");
}
}
[B]And the Exception:[/B]
Exception in thread "main" java.sql.SQLException: [Microsoft][ODBC Microsoft Acc
ess Driver] Too few parameters. Expected 1.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3111)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(JdbcOdbcStatement.java:2
53)
at db.store(db.java:22)
at database.main(database.java:51)
Please urgently need a solution.Thanks in advance