5 Replies - 16528 Views - Last Post: 30 November 2008 - 01:46 PM Rate Topic: -----

#1 dappaclick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-November 08

Java string variable sql insert

Post icon  Posted 30 November 2008 - 01:12 PM

I am trying to call in a variable instead of actual values when using the sql insert statement in my java program.
String value1 = new String("testing");

try{
		  
		Statement st = conn.createStatement();
		int val = st.executeUpdate("INSERT INTO pet(name,sex,dob) VALUES(value1, 'm','1998-11-11')");
		  


		  String SQL = ("INSERT INTO pet(name,sex,dob) VALUES("+value1+", 'm','1998-11-11')");
		  stmt.executeUpdate(SQL);



I have tried both of these and neither of them work.

Heres my sql table if it helps

+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(20)| NO | PRI | | |
| sex | char(1) | YES | | NULL | |
| dob | date | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+

+-------+------+------------+
| name | sex | dob |
+-------+------+------------+
| Amit | m | 1999-12-09 |
| chodo | f | 1542-11-01 |
+-------+------+------------+

Is This A Good Question/Topic? 0
  • +

Replies To: Java string variable sql insert

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,292
  • Joined: 20-September 08

Re: Java string variable sql insert

Posted 30 November 2008 - 01:20 PM

Use a PreparedStatement - it will take care of the quoting for you:

String sql= "INSERT INTO pet(name,sex,dob) VALUES(?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, value1);
ps.setString(2, "m");
ps.setDate(3, java.sql.Date.valueOf("1998-11-11"));
int val = ps.executeUpdate();
// close everything


This post has been edited by g00se: 30 November 2008 - 01:22 PM

Was This Post Helpful? 0
  • +
  • -

#3 dappaclick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-November 08

Re: Java string variable sql insert

Posted 30 November 2008 - 01:29 PM

Thanks for your reply
but im having problems with the line:
ps.setDate(java.sql.Date.valueOf("1998-11-11"));  



Compile error says :
The method setDate(int, Date) in the type PreparedStatement is not applicable for the arguments (Date)
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • Joined: 14-September 07

Re: Java string variable sql insert

Posted 30 November 2008 - 01:30 PM

edit: goose beat me to it and I am a loser


Since you have a string already made, why not use a PreparedStatement?

 Connection conn=null;
  PreparedStatement stmt=null;
   ResultSet rset=null;
///
stmt=conn.prepareStatement(RETRIEVE_STMT);
stmt.setString(1,dep); //change for multiple parameters
 rset=stmt.executeQuery();

 String RETRIEVE_STMT = ("INSERT INTO pet(name,sex,dob) VALUES("+value1+", 'm','1998-11-11')");


This post has been edited by KYA: 30 November 2008 - 01:31 PM

Was This Post Helpful? 0
  • +
  • -

#5 dappaclick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-November 08

Re: Java string variable sql insert

Posted 30 November 2008 - 01:42 PM

I found the problems jus missed out the 3:

ps.setDate(java.sql.Date.valueOf("1998-11-11"));

ps.setDate(3, java.sql.Date.valueOf("1998-11-11"));

Thanks every1
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java string variable sql insert

Posted 30 November 2008 - 01:46 PM

View Postg00se, on 30 Nov, 2008 - 12:20 PM, said:

Use a PreparedStatement - it will take care of the quoting for you:

Will also take care of the missing ; at the end of the SQL statement
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1