Ciao a tutti!
Ho creato un'applicazione in Java che mi permette di connettermi ad un server.
Creato il database da schermata PHP, devo aggiungere all'applicazione un metodo per formulare una query tramite la quale creo una tabella se non esiste.
Questo è il codice,
codice:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class testconn{
public static void main(String[] args) {
String indirizzo;
String nome;
String pass;
Scanner in;
String riga;
in= new Scanner (System.in);
System.out.println("Inserisci indirizzo database: ");
indirizzo=in.nextLine();
System.out.println("Inserisci nome database: ");
nome=in.nextLine();
System.out.println("Inserire password: ");
pass=in.nextLine();
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch (Exception exc1)
{
System.out.println("Errore - Driver jdbc non presente: "+
exc1.getMessage());
}
try
{
Connection conn = DriverManager.getConnection
(
"jdbc:mysql://"+indirizzo,
nome,
pass
);
/* restituzione data e ora */
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("SELECT now();");
while (rset.next())
{
System.out.println(rset.getString(1));
}
/* creazione tabella */
Statement stmt1 = conn.createStatement();
String sql = CREATE TABLE LSVDB.dipendenti(" id INTEGER NOT NULL, nome CHAR(50) NOT NULL, cognome CHAR(50) NOT NULL, ruolo CHAR(20) NULL, assunzione DATETIME NOT NULL");
stmt.executeUpdate(sql);
rset.close();
stmt.close();
stmt1.close();
conn.close();
}
catch (Exception exc)
{
System.out.println("Errore: "+ exc.getMessage());
}
}
l'errore che mi segnala è il sguente:
codice:
Errore: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1