This is a stock management system and I have a problem with updating part in good received note. When we purchase goods, we get a good received note, so when we get the items the number of items has to be updated (has to be added to the remaining goods) according to the item code.
According to the following code data from GRN will be extracted and will be saved in GRN1 and GRN2 tables. There's another table called Item_Supplier, when we receive goods Stock in hand field in Item_Supplier table has to updated according to the item_Id as well.
Problem is following code does not work.
Connection ss = Class_DB.myconnection();
Statement st = ss.createStatement();
-
ResultSet rs = st.executeQuery("select stock_in_hand from item_supplier where item_ID =('" + TF_GRN_ITEMID.getText() + "')");
grniid = TF_GRN_NO_OF_ITEM.getText();
while (rs.next()) {
nit = rs.getString("stock_in_hand");
}
sumn = grniid + nit;
st.executeUpdate("insert into grn1 values('" + TF_GRN_GRNNO.getText() + "','" + TF_GRN_SUPPLIERID.getText() + "','" + TF_GRN_AMOUNT.getText() + "','" + TF_GRN_DATE.getText() + "')");
st.executeUpdate("insert into grn2 values('" + TF_GRN_GRNNO.getText() + "','" + TF_GRN_ITEMID.getText() + "','" + TF_GRN_EXP_DATE.getText() + "','" + TF_GRN_TAX.getText() + "','" + TF_GRN_NO_OF_ITEM.getText() + "','" + TF_GRN_GAMOUNT.getText() + "','" + TF_GRN_NAMOUNT.getText() + "','" + TF_GRN_QTY.getText() + "','" + TF_GRN_UNIT.getText() + "','" + TF_GRN_FREE.getText() + "','" + TF_GRN_DIS.getText() + "')");
st.executeUpdate("update item_supplier set stock_in_hand='" + sumn + "' where item_ID='" + TF_GRN_ITEMID.getText() + "'");
JOptionPane.showMessageDialog(null, "Data Saved");
Please help.
Related
I wrote a query
sql = "INSERT INTO TABLE(COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5) " +
"VALUES('" + VAR1+ "','" + VAR2 + "','" + VAR3 + "','" + " '" + VAR4 + "','" + "VAR5 + );";
where var5 is a string.
When I try to insert into table and use above SQL I get the error
java.sql.SQLSyntaxErrorException: ORA-00917 : no comma
Where is the mistake?
You had the quotes wrong:
sql = "INSERT INTO TABLE(COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5) " +
"VALUES('" + VAR1 + "','" + VAR2 + "','" + VAR3 + "','" + VAR4 + "','" + VAR5 + ");";
The quote next to last must be placed between the plus and the parenthesis, not before VAR5. Also you doubled the single quote before VAR4 (credit to DevilsHnd).
Your single and double quotes around VAR5 was not correct and also you don't need ; in the string:
sql = "INSERT INTO TABLE(COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5) " +
"VALUES('" + VAR1+ "','" + VAR2 + "','" + VAR3 + "','" + " '" + VAR4 + "','" + VAR5 + "')";
Thanks for your help
The right syntax is:
sql = "INSERT INTO TABLE(COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5) " +
" VALUES('" + VAR1+ "','" + VAR2+ "','" + VAR3+ "','" + VAR4+ "','" + VAR5+ "')";
last week we got an assignment at school to develop a web Application for students, so they can use them to organize drives to school. The whole project is focused on project management and not on the programming part.
In my team we decided to make a GWT-Application because were all JAVA-Developers.
On the Server side I'm having trouble with our Microsoft Access DB I need to Insert a given dataset in the database, from the frontend I get an createObject which contains all the Information needed to be Insertet. My method looks like this:
#Override
public void makeOffer(CreateObject createobject) {
SecurityContext securityContext = SecurityContextHolder.getContext();
if(!securityContext.getAuthentication().isAuthenticated()) {
return;
}
String userName = securityContext.getAuthentication().getPrincipal().toString();
String teacher = "n";
if(userName.length() < 3) {
teacher = "y";
}
String sql = "INSERT INTO T_FAHRTEN "
+ "(F_SEATS, F_FREESEATS, F_CAR, F_TIME, F_SMOKE, F_GENDER, F_TEACHER, F_INFOS, F_NAME, F_VORNAME, F_NUMMER, F_MAIL, F_STARTTOWN, F_STARTSTREET, F_STARTPLZ, F_DATE, F_USERNAME)"
+ "Values ("
+ "'" + createobject.getSeats() + "', "
+ "'" + createobject.getFreeSeats() + "', "
+ "'" + createobject.getCar() + "', "
+ "'" + createobject.getTime() + "', "
+ "'" + createobject.getSmoke() + "', "
+ "'" + createobject.getGender() + "', "
+ "'" + teacher + "', "
+ "'" + createobject.getInfo() + "', "
+ "'" + createobject.getName() + "', "
+ "'" + createobject.getVorname() + "', "
+ "'" + createobject.getTelNummer() + "', "
+ "'" + createobject.getEmail()+ "', "
+ "'" + createobject.getStartStadt() + "', "
+ "'" + createobject.getStartStreet() + "', "
+ "'" + createobject.getStartPlz()+ "', "
+ "'" + createobject.getDate() + "', "
+ "'" + userName + "'"
+ ");";
Connection conn = null;
Statement stm = null;
try {
//FIXME
String dbURL = getClass().getResource("Drive2Gether2School1.accdb").getPath();
conn = DriverManager.getConnection("jdbc:ucanaccess://" + dbURL);
stm = conn.createStatement();
stm.executeUpdate(sql);
} catch (SQLException e) {
System.out.println("==> error creating");
e.printStackTrace();
//TODO implement Error Handling
} finally {
try {
if(stm != null) stm.close();
if(conn != null) conn.close();
} catch (SQLException e) {
System.out.println("==> error closing");
//TODO implement Error Handling
}
}
System.out.println("==> offer createt");
}
The Problem is, the data doesnt't get Insertet into the Access DB.
Actual its kinda weird, I have a method which gives me everything from the table and shows it on the Frontend. And If I insert something and then show everything I see the stuff I just insertet, but if I open the Tabel in MS-Access the Inserts are not there.
I already tried conn.commit and conn.setAutoCommit(true) both not helping.
I am working with mysql and Netbeans java for my school project. Whenever I try to register the details to the sql, I get this error My SqlSyntaxErrorException:Unknown Column " [the data in the text field] in 'field list'"
Here's my code:
int age = Integer.parseInt(AgeTF.getText());
String name=NameTF.getText();
String id=IDTF.getText();
String dob=DobTF.getText();
String address=AddressTF.getText();
try {
Class.forName("java.sql.Driver");
String database = "jdbc:mysql://localhost:3306/final";
Connection conn = DriverManager.getConnection(database, "root", "sanchit");
Statement stmt = conn.createStatement();
String sql = "insert into aadhar values ( '" + id + "', " + name + ", '" + dob + "' , '" + age + "' , '" + address + "' );" ;
stmt.executeUpdate(sql);
}
catch( Exception e){
JOptionPane.showMessageDialog(null,"" + e);
}
JOptionPane.showMessageDialog(this,"You have been registered!");
Please help.
Thanks
You didn't protect name with simple quotes :
Write '" + name + "' instead of " + name + "
String sql = "insert into aadhar values ( '" + id + "', '" + name + "', '" + dob + "' , '" + age + "' , '" + address + "' );" ;
Need help with this update query on JAVA, just started learning this but having problems
Getting following error upon execution
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing
operator) in query expression 'B.AWHERE ID =4'.
and data is not updating in MS Access database file
public void update(Student s)
{
int w = Integer.parseInt(s.getID());
String query = "UPDATE Student SET ID =" + w + "," + "FirstName =" + s.getFirstName() + "," + "LastName =" + s.getLastName() + "," + "Address =" + s.getAddress() + "," + "Gender =" + s.getGender() + "," + "DOB =" + s.getDOB() + "," + "Degree =" + s.getDegree() + "WHERE ID =" + w;
try
{
stmt.executeUpdate(query);
}
catch(SQLException e)
{
System.out.println("Problem in Query");
e.printStackTrace();
}
}
Change your UPDATE statement to be like below
String query = "UPDATE Student SET "FirstName = '" + s.getFirstName() +
"'," + "LastName = '" + s.getLastName() +
"'," + "Address = '" + s.getAddress() +
"'," + "Gender = '" + s.getGender() +
"'," + "DOB = '" + s.getDOB() +
"'," + "Degree = '" + s.getDegree() +
"' WHERE ID = " + w;
But your query won't make much sense cause, you are setting ID = w and also checking WHERE ID = w
You are missing spaces and apostrophes for string values in your whole query E.g.
"Degree =" + s.getDegree() + "WHERE ID ="
is being evaluated to
"Degree =BAWHERE ID ="
which is invalid syntax.
EDIT: I can only guess that your Student object returns strings in the getter methods so try this.
String query = "UPDATE Student SET ID =" + w + ","
+ "FirstName =\"" + s.getFirstName() + "\","
+ "LastName =\"" + s.getLastName() + "\","
+ "Address =\"" + s.getAddress() + "\","
+ "Gender =\"" + s.getGender() + "\","
+ "DOB =\"" + s.getDOB() + "\","
+ "Degree =\"" + s.getDegree() + "\" "
+ "WHERE ID =" + w;
I am having a small problem while I try to insert some data in my database (MS - SQL).
I am using this code (I just copy-paste all of it, so someone might understand smth that I don't)
try
{
Connection connection = null;
Statement Statement = null;
ResultSet ResultSet = null;
String query = "";
// jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
String host = "jdbc:sqlserver://server;databaseName=db";
String username = "user";
String password = "pass";
Statement = connection.createStatement();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
DateFormat dateFormat1 = new SimpleDateFormat("dd.MM.yyyy");
Calendar cal1 = Calendar.getInstance();
for (int k = 0; k < idArray.length ; k ++ )
{
query = "INSERT INTO Companies (ID,Company,Company2,Company3,Country,ZIP,City,Street," +
"Phone,Fax,Email,Internet,CustomerNo,AccountMngr,Icon,AddressSource," +
"UserDefined1,UserDefined2,CreatedOn,CreatedBy,ColectedInformation) " +
" VALUES ('" + UUID.randomUUID() + "','" + company_nameArray[k]
+ "','" + company_name2Array[k] + "','" + company_name3Array[k] + "','DE','"
+ zipArray[k] + "','" + cityArray[k] + "','" + streetArray[k] + "','"
+ phone_Array[k] + "','" + faxArray[k] + "','" + emailArray[k] + "','"
+ internetArray[k] + "','" + customer_noArray[k]
+ "','d','131','60','Baufinder Import','Import Datum "
+ dateFormat1.format(cal1.getTime()) + "','"
+ dateFormat1.format(cal1.getTime()) + "','d','"
+ "cxcxvcx" +
"')";
ResultSet = Statement.executeQuery(query);
}
connection.close();
}
catch(NullPointerException e)
{
System.out.println("NullPointerException");
}
catch ( SQLException err )
{
System.out.println( "SQL Exception:" ) ;
// Loop through the SQL Exceptions
while( err != null )
{
System.out.println( "State : " + err.getSQLState() ) ;
System.out.println( "Message: " + err.getMessage() ) ;
System.out.println( "Error : " + err.getErrorCode() ) ;
err = err.getNextException() ;
}
}
catch( Exception e )
{
System.out.println( "There is a problem with " + e ) ;
}
}
and I am getting this error:
SQL Exception: State : null
Message: There was no result set returned by the statement.
Error : 0
I don't understand the problem with the result test. I have been using it a lot, since I try to read the data from another database and now I try to insert them to another.
Furthermore, all of these tables do not include null values.
Thank you in advance:)
Use Statement#executeUpdate(String) instead of Statement#execute(query);
for (int k = 0; k < idArray.length ; k ++ )
{
query = "INSERT INTO Companies (ID,Company,Company2,Company3,Country,ZIP,City,Street," +
"Phone,Fax,Email,Internet,CustomerNo,AccountMngr,Icon,AddressSource," +
"UserDefined1,UserDefined2,CreatedOn,CreatedBy,ColectedInformation) " +
" VALUES ('" + UUID.randomUUID() + "','" + company_nameArray[k]
+ "','" + company_name2Array[k] + "','" + company_name3Array[k] + "','DE','"
+ zipArray[k] + "','" + cityArray[k] + "','" + streetArray[k] + "','"
+ phone_Array[k] + "','" + faxArray[k] + "','" + emailArray[k] + "','"
+ internetArray[k] + "','" + customer_noArray[k]
+ "','d','131','60','Baufinder Import','Import Datum "
+ dateFormat1.format(cal1.getTime()) + "','"
+ dateFormat1.format(cal1.getTime()) + "','d','"
+ "cxcxvcx" +
"')";
int updatedRowCount = Statement.executeUpdate(query);
}
Notes:
Please consider using PreparedStatement to prevent possibility of SQL Injection if some data directly input by User.
Try to use batch insert if you need to improve performance of inserting.