Date in java

Forum destinado a Linguagem JAVA! Classes, orientação a objeto, conexão com banco, chamada de procedures, etc
Locked
george15
Rank: Estagiário Pleno
Rank: Estagiário Pleno
Posts: 6
Joined: Fri, 26 Nov 2010 2:01 pm
Location: Manaus

Then guys, I have a problem to insert a date by Java if.

This is SQL to insert directly to the bank:

Select all

insert into venda(vendata,venclicodigo,venfuncodigo) values (to_date('28/08/98  2:43:22','dd/mm/rr hh24:mi:ss'),2,2);
But how to make Java insert the date?

Select all

 rs = st.executeQuery("Insert into venda(vendata,venclicodigo,venfuncodigo) values ('"+vendata+ "','"+venclicodigo+"','"+venfuncodigo+"')");
burga
Rank: DBA Pleno
Rank: DBA Pleno
Posts: 232
Joined: Thu, 26 Nov 2009 1:05 pm
Location: SP
Ricardo H. Tajiri

Hi George,

Concatenation of values ??always allows SQL Injection ... Be careful with it.
Other than that, pro Java, you must inform at least lib that you are using, and the form of connection to the bank, otherwise it is difficult to help you.

I will assume that you are using direct JDBC and lib java.sql, by the line of code you passed. Then, your Java connection with Oracle must be using the Java.sql.connection interface.

Come on, let's say an object con use the interface mentioned above (java.sql.connection) and the connection is already open and configured. So the right thing would be to use Java.sql.PreparedStatement to assemble and run your insert.

Select all

PreparedStatement stmt = con.prepareStatement("Insert into venda(vendata,venclicodigo,venfuncodigo) values (?,?,?)"); 
stmt.setDate(1, vendata); 
stmt.setString(2, venclicodigo); 
stmt.setString(3, venfuncodigo); 
stmt.execute();
See that the date is set with the setdate, the other parameters I put as string, but if they are not, you exchange the correct type. . This way you can run any SQL, and still "treats" the problem of SQL Injection.

How do I make a time I do not move xom the java, so you can have some mistakes, but the way is this one ...

If you can not , put there, but I think it best to move the topic to the Java Forum, because the subject here is no longer about SQL and yes about Java.
george15
Rank: Estagiário Pleno
Rank: Estagiário Pleno
Posts: 6
Joined: Fri, 26 Nov 2010 2:01 pm
Location: Manaus

Ok Ricardo, you're right ... My question was even in Java, but I'll post here a solution that I found ...
victorhugomuniz
Moderador
Moderador
Posts: 1396
Joined: Fri, 01 Feb 2008 2:06 pm
Location: Rio de Janeiro - RJ
Contact:
:D

Duplicate topic locked
Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 10 guests