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.
Related
I recently added a column to my SQLite database, since adding this column a button which I had which sets the value of one of the database columns to "1" or "0" have now started to crash my application when the button tries to use the updateData(), so I assume thats where my issue is, is in the code or the syntax of the updateData()
Error:
android.database.sqlite.SQLiteException: near "WHERE": syntax error
(code 1 SQLITE_ERROR): , while compiling: UPDATE my_manager SET
location_name = 'blarney stone' , location_county =
'Cork',location_description = 'jj',location_route =
'1',location_position = '2',location_longg = 'null',location_lat =
'null',location_url = 'JJ',location_url2 = 'jj',location_url3 = 'jj',
WHERE location_id = '1'
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native
Method)
Code:
void updateData(String id, String row_id, String name, String county, String description, String
in_route, String position, String lat, String longg, String url, String url2, String url3) {
System.out.println(TABLE_NAME+"; "+row_id +"; "+ name +"; "+ county+"; "+ description+"; " + lat
+"; "+ longg+"; "+ url +"; "+ url2 +"; "+ url3 +"; ");
SQLiteDatabase db = this.getReadableDatabase();
db.execSQL("UPDATE " + "my_manager" + " SET location_name = "+"'"+ name + "' " + ", " +
"location_county = " + "'"+ county + "'"+ "," +
"location_description = " + "'"+ description + "'" + "," +
"location_route = " + "'"+ in_route + "'" + "," +
"location_position = " + "'"+ position + "'" + "," +
"location_longg = " + "'"+ longg + "'" + "," +
"location_lat = " + "'"+ lat + "'" + "," +
"location_url = " + "'"+ url + "'" + "," +
"location_url2 = " + "'"+ url2 + "'" + "," +
"location_url3 = " + "'"+ url3 + "'" + "," + " WHERE location_id = "+"'"+ row_id+"'");
Sorry my friend don't take this as a bad but how about you avoid to much unnecessary "+" if it is possible.
void updateData("UPDATE my_manager SET location_name = '"+ name + "', " +
"location_county = '"+ county + "', " +
"location_description = '"+ description + "'," +
"location_route = '"+ in_route + "', " +
"location_position = '" + position + "', " +
"location_longg = '" + longg + "'," +
"location_lat = '" + lat + "'," +
"location_url = '" + url + "', " +
"location_url2 = '"+ url2 + "', " +
"location_url3 = '"+ url3 + "' WHERE location_id = '"+ row_id+"'");
I am trying to update my new database but for some reason after executing my Prepared Statement the data remains the same. It returns no errors or any exceptions. Please have a look at my code and see if you can help me. Thanks in advance.
try {
connect();
PreparedStatement update = con.prepareStatement("UPDATE generalInfo SET "
+ "site" + " = '"
+ siteTt + "' , "
+ "area" + " = '"
+ areaTt + "' , "
+ "unit" + " = '"
+ unitTt + "' , "
+ "unitName" + " = '"
+ unitNameTt + "' , "
+ "drawing" + " = '"
+ drawingTt + "' , "
+ "system" + " = '"
+ systemTt + "' , "
+ "stream" + " = '"
+ streamTt + "' , "
+ "product" + " = '"
+ productTt + "' , "
+ "equipmentLoc" + " = '"
+ equipLocTt + "' , "
+ "specificLoc" + " = '"
+ specificLocTt + "' , "
+ "camOperator" + " = '"
+ camTechTt + "' , "
+ "camSerial" + " = '"
+ camSerialTt + "' , "
+ "gasSurveyOperator" + " = '"
+ surveyTechTt + "' , "
+ "gasSurveySerial" + " = '"
+ surveySerialTt + "' , "
+ "equipmentDesc" + " = '"
+ equipDescTt + "' , "
+ "equipmentType" + " = '"
+ equipTypeTt + "' , "
+ "equipmentSize" + " = '"
+ equipSizeTt + "' , "
+ "equipmentID" + " = '"
+ equipIDTt + "' , "
+ "[Maintenance type]" + " = '"
+ repairTt + "' , "
+ "measurementPosition" + " = '"
+ sourceTt + "'"
+ " WHERE leakerID = " + Integer.parseInt(leakerIDCombo.getSelectedItem().toString())+";");
update.closeOnCompletion();
update.executeUpdate();
con.close();
System.out.println("saved");
} catch (SQLException ex) {
Logger.getLogger(RefineryData.class.getName()).log(Level.SEVERE, null, ex);
}
never forgett to commit your changes:
con.setAutoCommit(true);
update.closeOnCompletion();
update.executeUpdate();
con.close();
or
update.closeOnCompletion();
update.executeUpdate();
con.commit();
con.close();
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;
Why is the SQL not in the try catch, Problem is I tried using two separate try catches but then the SQL statement could not reference the FileInputStream variable and this was the alternative I came up with but it didn't seem to work
try {
final FileInputStream FIS = new FileInputStream(ReturnFile);
JButton Submit = new JButton ("Insert");
Submit.setLocation (430, 335);
Submit.setSize (150, 25);
newCarFrame.add(Submit);
Submit.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
Connection Con = new DataBaseHandler().GetConnection();
Statement stmt = Con.createStatement();
String SQL = "INSERT INTO Cars (`Reg_No`, `Car_Image`, `Make`, `Model`, `Mileage`, `Date_Of_Purchase`, `Price`, `Storage_Location`, `Body_Type`, `Gearbox`, `Age`, `No_Of_Doors`, `Colour`, `Fuel_Type`, `Engine_Size`, `Description`) VALUES ('" + RegNoTextBox.getText() + "', '" + FIS + "', '" + MakeComboBox.getSelectedItem() + "', '" + ModelTextBox.getText() + "', '" + MillageTextBox.getText() + "', '" + DOPField.getText() + "', '" + PriceField.getText() + "', '" + StorageCombo.getSelectedItem() + "', '" + BodyTypesCombo.getSelectedItem() + "', '" + GearBoxValue + "', '" + No_Of_Doors_Combo.getSelectedItem() + "', '" + AgeField.getText() + "', '" + ColourField.getText() + "', '" + PetrolValue + "', '" + EngineSizeField.getText() + "', '" + DescriptionField.getText() + "')";
System.out.println(SQL);
//Execute the SQL query
//TODO Salt the password
stmt.executeUpdate(SQL);
Con.close();
String ShowAllSalesSQL = "SELECT * FROM `Cars`";
SalesWindow MSW = new SalesWindow (ShowAllSalesSQL);
}
});
} catch (FileNotFoundException e2) {
System.out.println("No File");
e2.printStackTrace();
} catch (SQLException e1) {
}
All help is greatly appreciated thanks.
That's because when you do this Submit.addActionListener(new ActionListener () { ... } you are actually implementing a ActionListener interface inline, as anonymous class. In that try block you are only setting the action listener instance to Submit button, you are not actually surrounding the code which will be executed when the button is clicked.
Sorry I was being stupid I fixed it as shown below
JButton Submit = new JButton ("Insert");
Submit.setLocation (430, 335);
Submit.setSize (150, 25);
newCarFrame.add(Submit);
Submit.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
try {
final FileInputStream FIS = new FileInputStream(ReturnFile);
Connection Con = new DataBaseHandler().GetConnection();
Statement stmt = Con.createStatement();
String SQL = "INSERT INTO Cars (`Reg_No`, `Car_Image`, `Make`, `Model`, `Mileage`, `Date_Of_Purchase`, `Price`, `Storage_Location`, `Body_Type`, `Gearbox`, `Age`, `No_Of_Doors`, `Colour`, `Fuel_Type`, `Engine_Size`, `Description`) VALUES ('" + RegNoTextBox.getText() + "', '" + FIS + "', '" + MakeComboBox.getSelectedItem() + "', '" + ModelTextBox.getText() + "', '" + MillageTextBox.getText() + "', '" + DOPField.getText() + "', '" + PriceField.getText() + "', '" + StorageCombo.getSelectedItem() + "', '" + BodyTypesCombo.getSelectedItem() + "', '" + GearBoxValue + "', '" + No_Of_Doors_Combo.getSelectedItem() + "', '" + AgeField.getText() + "', '" + ColourField.getText() + "', '" + PetrolValue + "', '" + EngineSizeField.getText() + "', '" + DescriptionField.getText() + "')";
System.out.println(SQL);
//Execute the SQL query
//TODO Salt the password
stmt.executeUpdate(SQL);
Con.close();
String ShowAllSalesSQL = "SELECT * FROM `Cars`";
SalesWindow MSW = new SalesWindow (ShowAllSalesSQL);
} catch (SQLException e1) {
e1.printStackTrace();
} catch (FileNotFoundException e3) {
e3.printStackTrace();
}
}
});