Friday, January 19, 2018

Java MySQL DELETE (using Statement)

See also: MySQL XAMPP Connection for Java jdbc Program

Suppose we have folowing table:



Now we want to delete one record at a time by following:
  • Create a Java Connection to our MySQL database.
  • Create a SQL DELETE statement.
  • Then execute a Java Statement, using our SQL DELETE statement.
  • Close our Java MySQL database connection.
  • Finally catch any SQL exceptions using try-catch clause.


See also:
Reference

Java MySQL INSERT (using Statement)

See also: MySQL XAMPP Connection for Java jdbc Program

First suppose we have a table in out database named user. Or we can create a simple table like below:



Now we want to insert one record in this table. By following some steps we will do that.
  • Create a Java Connection to our MySQL database.
  • Create a SQL INSERT statement.
  • Then execute a Java Statement, using our SQL INSERT statement.
  • Close our Java MySQL database connection.
  • Finally catch any SQL exceptions using try-catch clause.


quey1 is passing direct values to the table. query2 is passing values through variables. Be careful about quote marks for executing queries.

See also:
Reference

Java MySQL SELECT (using Statement)

See also: MySQL XAMPP Connection for Java jdbc Program

Let's see the following table:



Now follow these steps:

  • Create a Java Connection to our MySQL database.
  • Define SQL SELECT statement.
  • Then execute the SELECT query, getting a Java ResultSet from that query
  • Now iterate over the ResultSet, getting the database fields (columns) from each row of data that is returned
  • Close our Java MySQL database connection.
  • Finally catch any SQL exceptions using try-catch clause.


See also:
Reference

MySQL XAMPP Connection for Java jdbc Program

First we will open our XAMPP server.





Then type localhost/phpmyadmin/ in the browser. After that we will create our database. After that, see below java code for setting up connection for the database.

Java Database Connectivity (JDBC) is an application programming interface (API) for the programming language Java, which defines how a client may access a database. It is Java based data access technology and used for Java database connectivity. It is part of the Java Standard Edition platform, from Oracle Corporation.




"jdbc:mysql://localhost:3306/newdatabase" ; here 3306 is the port number for XAMPP server and newdatabase is the name of our database.

Remember : First create the database, otherwise it won't connect simply by coding
These parts seem ok, but you will get error if you didn't add the appropriate library(ies) to the project using Netbeans's library facility. So, let's see how we can do that :
  • First Right-click on the project's root node in the Projects tab.
  • In the pop-up context menu, click on Properties (on the bottom of the menu).
  • Click on Libraries under Categories:. You should see the following screen:



  • Click on the Add Library...
  • Under Global Libraries click on MySQL JDBC Driver and then click the Add Library button. (If you don't have library named MySQL JDBC Driver, then simply download it from internet)
  • Finally Click on OK.
If you need a specific version of the driver, you can download it, and then after clicking on Add Library... you can click on Create... to add the downloaded version to your library repository. You'd then remove the default JDBC Driver from the project, and add the library containing the specific version.
Following these steps properly will be enough for you to access your database.
See also:
 Reference

Java UDP (Transfer Any Types & Size of File)

In my previous post, I wrote about how to transfer files from one location to another using UDP connection for client server model. There was a limitation of how much size of data can be passed. Now I will discuss about how to pass any types and size of data using UDP connection.

As the file is larger, we can't send the file simply by using DatagramPacket with the help of our byte array. So what we need to do is that we will first divide the full file into some chunks or small size of byte array, say 1024 bytes each. Then we will count how many byte array is required to transfer the file fully. Then we will use a loop to send all the chunks serially.






We created a DatagramSocket ds, DatagramPacket out, byte array snd of size 1024. We created a File f1 and gave the location of the file to be transfered. Now for getting how many packets are required to transfer the file fully, we divided the file by the length of our byte array. So no_of_pkt will hold how many packets are needed to be sent. We then simply send the value to the server to let the server know that, the next file it will receive, will have no_of_pkt number of packets that are to be transfered.






We created a FileInputStream ff to transfer the packets at a time to the server. Inside the loop, we just created byte array of size 1024 each time of sending a new packet. The server will gradually receive these packets one at a time.

Thread.sleep(10L): Pausing Execution with Sleep. Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system. We need this as we have many packets to be send, we will consider each of the sending process as a new different thread.
The server will also receive packets with the same loop stated above and gradually write them in a new file.

Sometimes server can't write all the packets. Packets may loss at the time of transfering process. Client will send all the packets but because server can't get all the data ( approximately 10-20% loss ) we need to wrap our whole program in a try-catch for exception. As each time we are sending and receiving new FileStream, we need to check whether they contain data from the previous packet or not. If they have data from the previous packet, then these types of data loss occurs. We need to handle it accordingly. Simply nullifying the Stream before each packet ( both sending and receiving ) will help.