cancel
Showing results for 
Search instead for 
Did you mean: 

why this doesn't work, saving data in the mysql database

Former Member
0 Kudos

Hi, I need an advice on code below. I made an db named "DBname" and inside table named "Date1". However I still fail to write anything from inputfield.


public void onActionSave(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    //@@begin onActionSave(ServerEvent)
	
	
	try {
	Class.forName("com.mysql.jdbc.Driver").newInstance();

	String url="jdbc:mysql://adress/DBname";
	Connection con=DriverManager.getConnection(url,"user","password");
	System.out.println ("Database connection established");
	
	Date Date = wdContext.currentContextElement().getDate();  //Date = name of Context binded to Date IF

	String sql = "insert into Date1 values (?)";
	PreparedStatement stmt = con.prepareStatement(sql);
	stmt.setDate(1, Date);

	} catch (Exception e) {
//	   TODO Auto-generated catch block
	e.printStackTrace();
	}
}

After pressing "Save" button application starts "working" (the round coursor is moving) for around ~20seconds.

Please advice couse I have run out of ideas. Regards, Balmer

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Well i feel the execute statement is missing here.


stmt.executeUpdate();

thanks & regards,

Manoj

Former Member
0 Kudos

f**k me, how pssibly could I missed it. That was it. Many thanks.

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi,

Try this:


try {
	Class.forName("com.mysql.jdbc.Driver").newInstance();
 
	String url="jdbc:mysql://adress/DBname";
	Connection con=DriverManager.getConnection(url,"user","password");
	System.out.println ("Database connection established");
	
	Date Date = wdContext.currentContextElement().getDate();  //Date = name of Context binded to Date IF
 
	String sql = "insert into Date1 values (?)";
	PreparedStatement stmt = con.prepareStatement(sql);
	stmt.setDate(1, Date);

        stmt.executeUpdate();

        stmt.close();
        con.close();
     } catch (Exception e) {
              e.printStackTrace();
     }

If this doesn't work try debugging and check if the connection is fine.

Regards,

Satyajit.

Former Member
0 Kudos

Hi,

The code seems to be fine. did u try checking the exception, if it is getting caught?

try including the column name also in the query, even there is only one column in the table. I know it sounds a bit strange, but long back even i was facing a similar issue. so had to change the query a bit. just give it a try and let me know if it is working

insert into <table name> (<col1>, <col2>) values (?,?)

Regards,

Gita

Former Member
0 Kudos

Hi, thank you for your help, I appreciate it. I did as you told but it didn't change anything. No errors are listed, no data written in the db. I fail to understand what is wrong here, problem is it has to work today... Got any other idea? Even slightest would be most welcome. Regards, Balmer.