peppepayens
14-06-2010, 18:28
Prima di tutto buona sera a tutti
allora io devo fare un progetto che può inserire ricercare e cancellare oggetti in un database...
nello specifico il mio database deve essere formato di oggetti di una classe creata da me di nome CD un'altra DVD e poi un altra Libro...
è un progetto client server multithread con interfaccia grafica e database mysql...
allora mi sono bloccato in un punto, praticamente ho creato manualmente il database in mysql ed ho aggiunto la tabella inserisciCD dove poi posso inserire oggetti di tipo CD stessa cosa per DVD e per Libro(mi manca ora solo la parte di ricerca nel database e di cancellazioni di oggeti nel database).
ho collegato il mio bottone salva ad un action listener che manda attraverso il metodo Client.inserisciCD(parametri da inserire) l'oggetto che il client vuole caricare nel database.
questo è il server ora metto il client e poi il pannello che gestisce l'aciotn listener
package Progetto;
import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import java.sql.*;
public class MultiThreadServer {
	
private static ServerSocket serverSocket;
private static final int PORT = 9878;
private static Connection con;
public static void main(String[] args) {
    	
try{
Class.forName("com.mysql.jdbc.Driver");
String nomeDB = "ArchivioDB";
String nomeUtente = "root";
String pwdUtente = "root";
con = DriverManager.getConnection("jdbc:mysql://localhost/" + nomeDB + "?user=" + nomeUtente + "&password=" + pwdUtente);
// boolean ris = con.createStatement().execute(Query.CreaArchivioCD);
// if (ris){
// System.out.println("Archivio CD");
// }
     
     
}catch(SQLException e) {
System.out.println("errore ");
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
         
try {
serverSocket = new ServerSocket(PORT);
System.out.println("Server in attesa...");
while(true) {
//Wait for client...
Socket client = serverSocket.accept();
System.out.println("\nNuovo client accettato.\n");
ServerThread thread = new ServerThread(client,con);
thread.start();
// Esecuzione dei thread in base alla richiesta dei client
     
}
}catch (IOException ioEx) {
System.out.println("\nUnable to set up port!");
ioEx.printStackTrace();
System.exit(1);
}
}
    
}
class ServerThread extends Thread {
Socket client;
ObjectInputStream input;
ObjectOutputStream output;
Connection con;
public ServerThread(Socket socket,Connection con ) {
//Set up reference to associated socket...
client = socket;
this.con=con;
try {
// input = new ObjectInputStream(client.getInputStream());
// output = new PrintWriter(client.getOutputStream(),true);
output = new ObjectOutputStream( socket.getOutputStream() );
input = new ObjectInputStream( socket.getInputStream() );
	       
}
catch(IOException ioEx) {
ioEx.printStackTrace();
}
}
	    
public void run() {
try {
Object obj=input.readObject();
// if(obj instanceof CD)
if(obj.getClass().getName().equals("CD")){
CD cd = (CD)obj;
PreparedStatement ps = con.prepareStatement(Query.InsertCD);
ps.setString(1,cd.getTitolo());
ps.setString(2,cd.getAutore());
ps.setInt(3,cd.getNumeroTracce());
ps.setDouble(4,cd.getTempoEsecuzione());
ps.setBoolean(5,cd.getPrestito());
ps.setString(6,cd.getCommento());
int res = ps.executeUpdate();
if(res>0){
output.writeObject( true );
output.flush();
;
} else
output.writeObject( true );
output.flush();
ps.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
allora io devo fare un progetto che può inserire ricercare e cancellare oggetti in un database...
nello specifico il mio database deve essere formato di oggetti di una classe creata da me di nome CD un'altra DVD e poi un altra Libro...
è un progetto client server multithread con interfaccia grafica e database mysql...
allora mi sono bloccato in un punto, praticamente ho creato manualmente il database in mysql ed ho aggiunto la tabella inserisciCD dove poi posso inserire oggetti di tipo CD stessa cosa per DVD e per Libro(mi manca ora solo la parte di ricerca nel database e di cancellazioni di oggeti nel database).
ho collegato il mio bottone salva ad un action listener che manda attraverso il metodo Client.inserisciCD(parametri da inserire) l'oggetto che il client vuole caricare nel database.
questo è il server ora metto il client e poi il pannello che gestisce l'aciotn listener
package Progetto;
import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import java.sql.*;
public class MultiThreadServer {
private static ServerSocket serverSocket;
private static final int PORT = 9878;
private static Connection con;
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
String nomeDB = "ArchivioDB";
String nomeUtente = "root";
String pwdUtente = "root";
con = DriverManager.getConnection("jdbc:mysql://localhost/" + nomeDB + "?user=" + nomeUtente + "&password=" + pwdUtente);
// boolean ris = con.createStatement().execute(Query.CreaArchivioCD);
// if (ris){
// System.out.println("Archivio CD");
// }
}catch(SQLException e) {
System.out.println("errore ");
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
serverSocket = new ServerSocket(PORT);
System.out.println("Server in attesa...");
while(true) {
//Wait for client...
Socket client = serverSocket.accept();
System.out.println("\nNuovo client accettato.\n");
ServerThread thread = new ServerThread(client,con);
thread.start();
// Esecuzione dei thread in base alla richiesta dei client
}
}catch (IOException ioEx) {
System.out.println("\nUnable to set up port!");
ioEx.printStackTrace();
System.exit(1);
}
}
}
class ServerThread extends Thread {
Socket client;
ObjectInputStream input;
ObjectOutputStream output;
Connection con;
public ServerThread(Socket socket,Connection con ) {
//Set up reference to associated socket...
client = socket;
this.con=con;
try {
// input = new ObjectInputStream(client.getInputStream());
// output = new PrintWriter(client.getOutputStream(),true);
output = new ObjectOutputStream( socket.getOutputStream() );
input = new ObjectInputStream( socket.getInputStream() );
}
catch(IOException ioEx) {
ioEx.printStackTrace();
}
}
public void run() {
try {
Object obj=input.readObject();
// if(obj instanceof CD)
if(obj.getClass().getName().equals("CD")){
CD cd = (CD)obj;
PreparedStatement ps = con.prepareStatement(Query.InsertCD);
ps.setString(1,cd.getTitolo());
ps.setString(2,cd.getAutore());
ps.setInt(3,cd.getNumeroTracce());
ps.setDouble(4,cd.getTempoEsecuzione());
ps.setBoolean(5,cd.getPrestito());
ps.setString(6,cd.getCommento());
int res = ps.executeUpdate();
if(res>0){
output.writeObject( true );
output.flush();
;
} else
output.writeObject( true );
output.flush();
ps.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}