[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 597
  • Last Modified:

javascript question for inserting record in sql server

I'm trying to use this code and I get an error  saying:

Error in connector "Database Writer" at Javascript:
Error on line 5: missing ; before statement


It is hard to tell here but line 5 is where I assign the var sql= statement.

the variables ${FirstName} are predifined variables in the MirthConnect application. Just know that they contain the info needed.



var dbConn = DatabaseConnectionFactory.createDatabaseConnection ('net.sourceforge.jtds.jdbc.Driver','jdbc:jtds:sqlserver://localhost:1433/Demographics','MirthUser','b9isnice');

if (MsgType == 'ADTA08')
{
  var sql="INSERT INTO Patient (PatientID,FirstName,MiddleName,LastName,Gender,SSN,HomePhone,HomeAddress1,City,State,Zip) values("+${PatientID}+",'"+${FirstName}+"','"+${MiddleName}+"','"+${LastName}+"','"+${Sex}+"','"+${myssn}+"','"+${HomePhone}+"','"+${Address1}+"','"+${City}+"','"+${State}+"','"+${Zip}+"')";

logger.error("Query is: " + sql);



var status=dbConn.executeUpdate(sql);



}


dbConn.close();

Open in new window

0
rutledgj
Asked:
rutledgj
1 Solution
 
celazkonCommented:
Try to use simple quotes instead of double quotes.
I don't program in Javascript now for some time, but from what I remember, this has been always an issue

Hope it helps, good luck
0
 
Justin MathewsCommented:
Braces - {} are not valid in a javascript variable name. So if the code above is javascript then those variables need to be changed. Are you sure it is  ${FirstName} and not $FirstName?
0
 
BadotzCommented:
Are you doing this on the client? Have you given a thought to security?
0
 
rutledgjAuthor Commented:
OK. So I changed it to use diff variables and it doesn't complain about line 5. Now it says the syntac of the db driver is incorrect.  My code now looks like this:

 
var dbConn = DatabaseConnectionFactory.createDatabaseConnection('net.sourceforge.jtds.jdbc.Driver','jdbc:jtds:sqlserver://host:port/dbname','','');

dbConn.close();var dbConn = DatabaseConnectionFactory.createDatabaseConnection ('net.sourceforge.jtds.jdbc.Driver','jdbc:jtds:sqlserver://localhost:1433/Demographics','MirthUser','b9isnice');

var fname = $('FirstName');
var mname = $('MiddleName');
var lname = $('LastName');
var patientid = $('PatientID');
var gender = $('Sex');
var ssn = $('myssn');
var phone = $('HomePhone');
var addr = $('Address1');
var city = $('City');
var st = $('State');
var zip = $('Zip');

if (MsgType == 'ADTA08')
{
 
var sql="INSERT INTO Patient (PatientID,FirstName,MiddleName,LastName,Gender,SSN,HomePhone,HomeAddress1,City,State,Zip) values("+patientid+",'"+fname+"','"+mname+"','"+lname+"','"+gender+"','"+ssn+"','"+phone+"','"+addr+"','"+city+"','"+st+"','"+zip+"')";

logger.error("Query is: " + sql);



var status=dbConn.executeUpdate(sql);



}


dbConn.close();

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now