Client Class
public class Cliente {
private Socket cliente;
private DataInputStream entrada;
private DataOutputStream salida;
public String sendMSN(String msn)
{
String dirIP = "127.0.0.1";
String mensaje = "";
String mensaje2 = "";
try
{
cliente = new Socket(dirIP, 2000);
salida = new DataOutputStream(cliente.getOutputStream());
salida.writeUTF(msn);
entrada = new DataInputStream(cliente.getInputStream());
mensaje = entrada.readUTF();
System.out.println(mensaje);
//inicioSesion is = new inicioSesion();
//st = entrada.readUTF();
mensaje2 = entrada.readUTF();
cliente.close();
}
catch(Exception e)
{
System.out.println("No se pudo enviar el mensaje de parte del cliente");
}
return mensaje2;
} // fin del metodo
}
Server Class
public class Server
{
public static void main(String arg[])
{
ServerSocket ss = null;
Socket s;
String sending = " Hola Mundo ";
OutputStream os;
InputStream is;
try {
ss = new ServerSocket(5000);
} catch(IOException e) {
System.out.println("Error ..." + e);
}
while (true)
{
try {
s = ss.accept();
os = s.getOutputStream();
DataOutputStream dout = new DataOutputStream(os);
dout.writeUTF(sending);
is = s.getInputStream();
DataInputStream din = new DataInputStream(is);
System.out.println(din.readUTF());
s.close();
} catch(IOException e) {
System.out.println("Error ..." + e);
}
} // fin del while
} // fin del main
} // fin de la clase
Database Query Method
public String consultarDATOS() {
Connection c = null;
Statement stmt = null;
String datos = " ";
OutputStream os;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:" + nameBD);
c.setAutoCommit(false);
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM PERSONA");
while (rs.next()) {
int id = rs.getInt("p_id");
String nombre = rs.getString("p_nombre");
String apellido = rs.getString("p_apellido");
int edad = rs.getInt("p_edad");
String cedula = rs.getString("p_cedula");
System.out.println("ID = " + id);
System.out.println("NOMBRE = " + nombre);
System.out.println("APELLIDO = " + apellido);
System.out.println("EDAD = " + edad);
System.out.println("CEDULA = " + cedula);
System.out.println();
String id1 = Integer.toString(id);
String ed = Integer.toString(edad);
datos = id + "," + nombre + "," + apellido + "," + ed + "," + cedula + ",";
}
rs.close();
stmt.close();
c.close();
} catch (Exception e) {
return datos = "error";
}
return datos;
}
Insert Record Method
public void insertarBD(int id, String nombre, String contra, String apellido, int edad, String cedula) {
Connection c = null;
Statement stmt = null;
System.out.println(id + "," + nombre + "," + contra + "," + apellido + "," + edad + "," + cedula);
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\esther\\Documents\\Temporal\\LoginT\\prueba.db");
c.setAutoCommit(false);
if (c != null)
{
System.out.println("Conectado a la base en especial insertar");
}
System.out.println("Opened database successfully-3");
stmt = c.createStatement();
String sql = "INSERT INTO PERSONA (p_id, p_nombre, p_contraseña, p_apellido, p_edad, p_cedula) " +
"VALUES (" + id + ", '" + nombre + "', '" + contra + "', '" + apellido + "', " + edad + ", '" + cedula + "');";
System.out.println("llego aqui");
stmt.executeUpdate(sql);
stmt.close();
c.commit();
c.close();
} catch (Exception e) {
System.out.println("ERROR al insertar " + e.getMessage());
e.printStackTrace();
}
System.out.println("Records created successfully");
}
Connect to Database Method
public void conectarBD() { // METODO CONECTAR
Connection c = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:" + nameBD);
if (c != null)
{
System.out.println("Conectado :)");
}
} catch (Exception e)
{
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully-1");
}