Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8767903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:56:08+00:00 2026-06-13T16:56:08+00:00

Im trying to make a little server for my homework.This is very simple project

  • 0

Im trying to make a little server for my homework.This is very simple project yet i cant insert some variables (which i took from the client ,in an object form ,through serialization ) into the database .
It shows no errors! That’s what i find strange and also the client receive the response without problems.

my Server class is as the following :

package server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.ietf.jgss.Oid;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class Server {

public static void main(String[] args) throws Exception {

    HttpServer server = HttpServer.create(new InetSocketAddress(3333), 0);

    server.createContext("/", new MyHandler());
    server.setExecutor(null); // creates a default executor
    server.start();
}

static class MyHandler implements HttpHandler {
    public void handle(HttpExchange t) throws IOException {
        ObjectInputStream ios = new ObjectInputStream(t.getRequestBody());
        final String url = "jdbc:mysql://localhost/httpServer";

        final String user = "root";

        final String password = "";

        try {
            Send oin = (Send) ios.readObject();

            String response = "Kjo eshte nje pergjigje nga serveri! \n"
                    + "Clienti me id "
                    + oin.getId()
                    + " dhe me emer "
                    + oin.getName()
                    + " ka pasur "
                    + oin.getAmount()
                    + "$ ne llogarine e tij ,por me pas ka terhequr "
                    + oin.getPaid()
                    + "$ nga llogaria \n"
                    + "Kjo terheqe eshte ruajtur ne database dhe tani gjendja e re eshte "
                    + (oin.getAmount() - oin.getPaid()) + "$ \n";
            t.sendResponseHeaders(200, response.length());
            OutputStream os = t.getResponseBody();
            os.write(response.getBytes());
            os.close();
            int id = oin.getId();
            String emri = oin.getName();
            int amount = oin.getAmount();
            int paid = oin.getPaid();
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection(url, user,
                        password);

                try {
                    Statement s = con.createStatement();

                    s.executeUpdate("INSERT INTO person VALUES ('" + id
                            + "','" + emri + "','" + amount + "','" + paid
                            + "')");
                } catch (SQLException s) {

                    System.out
                            .println("Tabel or column or data type is not found!");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
   }

can you please help me ?
Or have any idea what the problem may is ?

Edit:
Maybe i am doing something wrong in the Client:

package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;

class Send implements Serializable {
// duhet te implementoje interfacin serizable ne menyre qe tja dergoj
// serverit
private static final long serialVersionUID = 1L;

public int getId() {
    return id;
}

public int getAmount() {
    return amount;
}

public int getPaid() {
    return paid;
}

int id = 1;
int amount = 2000;
int paid = 800;
String name = "Andi Domi";

public String getName() {
    return name;
}
    }

   public class Client {
public static void main(String[] args) throws Exception {

    try {

        URL url = new URL("http://localhost:3333");
        HttpURLConnection s = (HttpURLConnection) url.openConnection();
        s.setDoOutput(true);
        s.setDoInput(true);
        s.setRequestMethod("POST");
        s.setUseCaches(false);
        Send obj = new Send();
        ObjectOutputStream objOut = new ObjectOutputStream(
                s.getOutputStream());
        objOut.writeObject(obj);

        InputStream in = s.getInputStream();
        InputStreamReader isr = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(isr);
        int c;
        while ((c = br.read()) != -1) {
            System.out.print((char) c);
        }
        objOut.close();
        s.disconnect();
    } catch (IOException ex) {
        System.err.println(ex);
        System.err.print("gabimi eshte ketu");
    }
    }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:56:09+00:00Added an answer on June 13, 2026 at 4:56 pm

    After your executeUpdate statement you need to do.

    con.commit();
    

    to save the transaction.

    EDIT: Based on the chat discussion, we learned that the column named emri is actually Emri in the table and was throwing:

    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'emri' in 'field list' 
    

    Changing the name resolves the issue.


    Now unrelated to your problem, you should be using a PreparedStatement instead and should be closing your connection and statement

    try {
      PreparedStatement s = con.prepareStatement("INSERT INTO person(id, emri, amount, paid) VALUES (?,?,?,?)");
      s.setInt(1,id);
      s.setString(2,emri);
      s.setInt(3,amount);
      s.setInt(4,paid);
      int count =  s.executeUpdate();
      con.commit();
    } catch(Exception e){
      e.printStackTrace();
      //something bad happened rollback 
      //any uncommitted changes
      con.rollback();
    } finally {
      if (con != null) {
        con.close();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to make this to be a little jQuery plugin that
I'm trying to make a little client-server script like many others that I've done
I'm trying to make a simple chat server in Java. Now I have a
I'm trying to write a server in Java. I know very little Java. I've
Im trying to learn php by doing a little project using apache server. I
Trying to make a little script that will turn on server. I found few
I'm trying to make a little web layout. What I have so far is
I'm trying to make a little console application that is able to deal with
I'm trying to make a little game for the console, so I need to
Okay so I'm trying to make a little gag program that will run away

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.