SQLException : insertion de données dans une base de donnée
Bonjour,
j'aimerais passer les valeurs des variables situées dans la classe Test dans une base de donnée
voici ma classe Test.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
public class Test {
static Logger log = Logger.getLogger(SendSms.class.getName());
static String msg;
public static void main(String[] args) {
Send t= new Send();
t.init();
t.server = "http://127.0.0.1:8800/";
Scanner lectureClavier = new Scanner(System.in);
System.out.print("Veuillez entrez votre text \n");
msg = lectureClavier.nextLine();
t.sentid=1;
t.phonenumber = "+1234567890";
t.text = msg;
try{
Class.forName("org.postgresql.Driver");
System.out.println("Driver O.K.");
String url = "jdbc:postgresql://localhost:5432/db";
String user = "postgres";
String passwd = "12345";
Connection conn = DriverManager.getConnection(url, user, passwd);
System.out.println("Connexion effective !");
Statement st= conn.createStatement();
String sql= "INSERT INTO sender(rec_id, num_tel, message) VALUES ("+t.sentid+","+t.phonenumber+","+t.text+")";
st.executeQuery(sql);
log.debug("fermeture de la connexion");
conn.close();
st.close();
}
catch(SQLException e){
log.error(e.getMessage());
e.printStackTrace();
}
catch(ClassNotFoundException e){
log.error(e.getMessage());
e.printStackTrace();
}
}
} |
voici ma Base de donnée
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
CREATE TABLE sender
(
rec_id serial NOT NULL,
num_tel character varying(20) DEFAULT ''::character varying,
message text,
CONSTRAINT sender_pkey PRIMARY KEY (rec_id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE sender
OWNER TO postgres; |
voici l'exception
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| Veuillez entrez votre text
ceci est un test
Driver O.K.
Connexion effective !
- ERREUR: erreur de syntaxe sur ou près de « est »
Position*: 76
org.postgresql.util.PSQLException: ERREUR: erreur de syntaxe sur ou près de « est »
Position*: 76
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:403)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:283)
at com.services.Test.main(Test.java:50) |
voci un 2eme essai avec un text plus court
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
Veuillez entrez votre text
fffff
Driver O.K.
Connexion effective !
- ERREUR: la colonne « fffff » n'existe pas
Position*: 71
org.postgresql.util.PSQLException: ERREUR: la colonne « fffff » n'existe pas
Position*: 71
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:403)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:283)
at com.services.Test.main(Test.java:50) |