PDA

View Full Version : [Java]Programmino in Java...dov'è l'errore?


Diego_Armando
30-05-2004, 15:59
Ciao!

Ho scritto un programmino in Java per effettuare delle query su un database.

Compilandolo mi da:

C:\jena>javac jdbcpr.java
jdbcpr.java:12: illegal start of expression
throws IOException
^
jdbcpr.java:47: ';' expected
}catch ( Exception e ) { e.printStackTrace();}
^
2 errors



import java.sql.*;
import java.io.*;

class jdbcpr {


public static void main(String[] args)
{

String[] ricerca = new String[100];

throws IOException
{
BufferedReader br = new BufferedReader ( new InputStreamReader (System.in));
String str[] = new String[100];

System.out.println("Inserire stringa");
for(int i=0;i<100;i++) {
str[i] = br.readLine();
if ( str[i].equals ("stop")) break;
}

for ( int i=0;i<100;i++ ) {
if (str[i].equals("stop")) break;
ricerca[i] = str[i];
}
}
try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String URL = "jdbc:odbc:db1";

Connection con;

con = DriverManager.getConnection(URL);

Statement query = con.createStatement();

ResultSet result = query.executeQuery("Select '" + ricerca + "' from dbprova");

while (result.next ()) {
String nome =
result.getString("nome");
System.out.println(nome);
}
}catch ( Exception e ) { e.printStackTrace();}
}

}

Un'ultima cosa...come faccio a prendere una stringa in input da console e passarla alla query?

Grazie mille..

kingv
30-05-2004, 16:28
import java.sql.*;
import java.io.*;

class jdbcpr {


public static void main(String[] args) throws IOException
{

String[] ricerca = new String[100];

BufferedReader br = new BufferedReader ( new InputStreamReader (System.in));
String str[] = new String[100];

System.out.println("Inserire stringa");
for(int i=0;i<100;i++) {
str[i] = br.readLine();
if ( str[i].equals ("stop")) break;
}

for ( int i=0;i<100;i++ ) {
if (str[i].equals("stop")) break;
ricerca[i] = str[i];
}
}
try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String URL = "jdbc:odbc:db1";

Connection con;

con = DriverManager.getConnection(URL);

Statement query = con.createStatement();

ResultSet result = query.executeQuery("Select '" + ricerca + "' from dbprova");

while (result.next ()) {
String nome = result.getString("nome");
System.out.println(nome);
}
}catch ( Exception e ) { e.printStackTrace();}
}

}


non ho la possibilità di compilarlo qui ma dovrebbe essere a posto. ;)

Diego_Armando
31-05-2004, 19:15
Ho provato a compilare il codice che mi hai scritto e adesso mi da questo errore:

C:\jena>javac jdbcpr.java
jdbcpr.java:26: illegal start of type
try{
^
jdbcpr.java:44: <identifier> expected
}catch ( Exception e ) { e.printStackTrace();}
^
jdbcpr.java:47: 'class' or 'interface' expected
}
^
jdbcpr.java:48: 'class' or 'interface' expected
^
4 errors

kingv
31-05-2004, 19:48
togli la graffa prima del try

scusa ma non ho il compilatore qui ;)

Diego_Armando
01-06-2004, 13:52
Grazie mille!
Funziona...grazie ancora