Back Java JDBC CRUD Tutorial (SQL Insert, Select, Update and Delete Examples)

50-60
Created By: Code Java
Published At: 2019-06-01
Search Query: Java Delete Files
Tags: Java JDBC CRUD Java JDBC Java JDBC tutorial JDBC tutorial JDBC example JDBC CRUD example JDBC CRUD tutorial insert row in Java delete row in Java select rows in Java get row in Java jdbc insert row jdbc select rows jdbc delete row jdbc update row update row in Java JDBC insert JDBC update JDBC select JDBC delete
Learn how to write Java code that manages information in a database (MySQL) - Create, Retrieve, Update and Delete (CRUD operations) using JDBC (Java database connectivity).

For text-based tutorial, refer to the following article:
https://www.codejava.net/java-se/jdbc/jdbc-tutorial-sql-insert-select-update-and-delete-examples
logo Ask VidTutorAssistant
Comment
or
Question
Your name
Comments with generated responses
@ammarbadri1150
2022-07-18

sir how can i solve this
error: ';' expected

String sql = " insert into Book values(" + book name + ",'" + book id + "'," + page number +")";

^

Book.java:19: error: not a statement

String sql = " insert into Book values(" + book name + ",'" + book id + "'," + page number +")";

^
error: not a statement

String sql = " insert into Book values(" + book name + ",'" + book id + "'," + page number +")";





^
error String sql = " insert into Book values(" + book name + ",'" + book id + "'," + page number +")";

@CodeJava

variable names can't have whitespace. You should write bookName, pageNumber,...

VidTutorAssistant (GPT-4)

Hi @ammarbadri1150, you seem to have faced an issue with inserting values into the Book database. The error you're getting is because Java does not recognize "book name", "book id", and "page number" as variables. Variables in Java should not have spaces - they should be written as one word or joined with an underscore.

Here's how you should write your code:

```
String sql = "insert into Book values('" + bookName + "','" + bookId + "','" + pageNumber +"')";
```

In the above line, "bookName", "bookId", and "pageNumber" are variables that store corresponding values. Be sure to replace these with your own variable names, and those variables should be properly initialized before being used in the SQL query.

Remember to use the PreparedStatement if the values you're inserting are user inputs or dynamic in nature, as shown in the video tutorial, to prevent SQL injection attacks.

I hope this solves your problem. Happy coding!


@aishwaryakamble4604
2022-11-30

Thank you very much... For such a wonderful tutorial 😊✨🙏

@CodeJava

you're welcome. Glad it helped.

VidTutorAssistant (GPT-4)

Thank you so much @aishwaryakamble4604 for your kind words! I'm glad you found our JDBC tutorial helpful. We aim to make complex topics like database connectivity as simple as possible so that everyone can quickly grasp and apply them. Please continue watching our channel and if there are any specific topics you'd like us to cover in the future, do let us know. Happy Coding! ✨👨‍💻👍


@dharan9203
2021-04-02

Can u create a new database in JDBC by using Oracle database ? it's possible?

@CodeJava

I think no. You must use Oracle tool to create the database.

VidTutorAssistant (GPT-4)

Absolutely, it's possible to connect to an Oracle database using JDBC. Much of the process is similar to what you learned in the MySQL JDBC tutorial. You'll start by installing the oracle database and oracle JDBC driver. The differences lie mainly in the JDBC URL and the driver. For Oracle, the URL typically follows this pattern: `jdbc:oracle:thin:@localhost:1521:dbname`. And the driver class name for Oracle is `oracle.jdbc.driver.OracleDriver`. You can then follow the similar steps of creating connections and executing SQL statements as explained in the tutorial. Always remember to check for compatibility between your Oracle database version and JDBC driver version. Keep coding!


@huuquocdattran2877
2021-08-30

Hello Mr Nam Ha Minh . I 've bg in this exercise.
when i add a querry like INSERT INTO duplicate value in sql, in sql table it still shows sql value.
How to check the value is already in the sql table and How to resolve this bug .

@CodeJava

you can set unique = true for the column that should not have duplicate values. Then it will throw an exception if you're trying to insert the same value. Then you catch this exception and handle it properly. Make sense?

VidTutorAssistant (GPT-4)

Hello @huuquocdattran2877, thank you for your question.

To prevent duplicate entries in SQL, especially when using the "INSERT INTO" query, you should consider creating a UNIQUE constraint on the specific column or set of columns that should contain unique values. If a duplicate entry is detected, the SQL engine will return an error and won't perform the insert operation.

In Java code, it can be handled by catching the SQLException that the MySQL database throws when the UNIQUE constraint is violated:

```java
try {
//execute your SQL insert statement here
} catch (SQLIntegrityConstraintViolationException e) {
System.out.println("Duplicate entry found, entry not inserted.");
}
```

Alternatively, if you do not want to impose the UNIQUE constraint, you can execute a SELECT statement to see if the data already exists in the table before you perform the INSERT operation. However, this approach could have performance implications when you're dealing with lots of data.

I hope this helps. Feel free to ask if you have further questions. Happy coding!


@eltirad0
2021-02-20

Do you consider a good practice in the Update to send all the fields, including those that were not changed in the form? I've always had this doubt, or send to update individually only those were changed in the form

@CodeJava

I prefer updating all the fields even some are not changed. If you want to update only fields that have changed, then you will have to code some logic to check values are changed or not, which could increase the complexity of the code unnecessarily (think about the logic to detect changes of a group of 3 checkboxes, for example).

VidTutorAssistant (GPT-4)

Hi @eltirad0, that's a great question. Updating individual fields vs updating all fields in the form fundamentally depends on the requirement and context. If you're certain that only specific fields may change, updating those fields individually may reduce unnecessary data traffic and slightly improve performance. But if you are not certain which fields might change or if multiple fields can change at a time, it might be more convenient to update all fields, as done in our tutorial. This way you make sure no changes are missed out. However, always remember, efficiency and good database management practices should always be a priority in real-world applications. Thanks for your engagement!


@deepikabhogi6194
2021-09-07

Can u perform all the three SQL query operations in a single code.without changing it each time

@CodeJava

oh, it's impossible to execute all Insert, Select and Update in a single statement.

VidTutorAssistant (GPT-4)

Absolutely, @deepikabhogi6194! You can certainly write a single program to execute all three SQL query operations (Create, Update, Delete), without changing the code each time. This could involve creating a method for each operation and calling them all sequentially, or including an interface for user input to choose the operation they want to perform. To maintain simplicity and readability, it might be a good idea to separate these operations into different classes or methods. Stay tuned for more advanced tutorials where I'll tackle more complex projects like the one you're describing!


@LuizOtavio-zj9br
2020-03-22

TIME ZONE ERROR (SOLVED)



In my case there was an error with my time zone.



To solve it, I went back to Mysql, right-clicked on our "users" table, clicked on "select rows" and typed the command:



SET GLOBAL time_zone = '+4: 00';



Then I went back to the eclipse and it worked!

@CodeJava

Thanks for your contribution.

VidTutorAssistant (GPT-4)

Hi @LuizOtavio-zj9br, that's a great catch - time zone differences can surely cause unexpected hiccups in programming. Your solution to set the global time zone directly in MySQL is a good point for other viewers to note.
I appreciate your input and glad you found a resolution. Keep experimenting and sharing your knowledge, as it contributes to the growth of this community. Happy coding!


@princeprijwal8350
2021-11-07

Coding is very straight to the point. Loved it and keep it up

@CodeJava

Glad you loved it. Thanks for watching :)

VidTutorAssistant (GPT-4)

Thank you @princeprijwal8350! We're thrilled to hear that you found the video helpful. We always strive to keep our coding tutorials clear and directly applicable. Stay tuned for more Java tutorials and don't hesitate to reach out with any questions or topics you'd like to see next! Happy coding!


@thomashughes8550
2020-04-16

Thank you! This really helped.

@CodeJava

I'm so glad! Subscribe to this channel.

VidTutorAssistant (GPT-4)

You're welcome, @thomashughes8550! We're so glad to hear that you found the video helpful. If you have any improvements or specific topics in Java programming that you'd love for us to cover, feel free to share with us. Happy coding!


@khalidhossain3850
2020-11-06

you are the most understandable teacher for java jdbc. When i saw your vedio , I understand it first time ......
you are best teacher for JDBC.

@CodeJava

Thanks and welcome! Come to watch my videos more often. Cheers!

VidTutorAssistant (GPT-4)

Thank you so much for your kind words, @khalidhossain3850! It's wonderful to hear that you find our tutorials on Java JDBC helpful and easy to understand. We strive to make complex concepts simple and easy to digest. Stay tuned for more informative videos. Keep coding and learning!