$url = "mysql-java-rw.php"; $tipo = 'e'; $index = false; $pubblicita = true; $title = "Guida MySQL: Accesso a MySQL da Java"; include("../include/page_struct_initial.php"); include("../visite.php"); ?> /*include("../pubblicita/pubblicita_testa.php");*/ ?> include("../include/article_header.php"); write_article_header("Guida MySQL: Accesso a MySQL da Java", "19 Novembre 2007", "Database", "", 0, "g"); ?>
id int
cognome char(25)
nome char (25)
eta int
import java.sql.*;
public class rdata
{
public static void main(String argv[]) throws Exception
{
try
{
Class.forName("org.gjt.mm.mysql.Driver");
}
catch (Exception exc)
{
System.out.println("Errore - Driver jdbc non presente: "+
exc.getMessage());
}
try
{
Connection conn = DriverManager.getConnection(
"jdbc:mysql:///basedati",
"root",
"password"
);
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("SELECT * FROM dati WHERE eta > 18;");
while (rset.next())
{
System.out.println("ID "+rset.getInt("id"));
System.out.println("Cognome "+rset.getString("cognome"));
System.out.println("Nome "+rset.getString("nome"));
System.out.println("Età "+rset.getInt("eta"));
}
rset.close();
stmt.close();
conn.close();
}
catch (Exception exc)
{
System.out.println("Errore: "+ exc.getMessage());
}
}
}
id int
cognome char(25)
nome char (25)
eta int
import java.sql.*;
public class rdata
{
public static void main(String argv[]) throws Exception
{
try
{
Class.forName("org.gjt.mm.mysql.Driver");
}
catch (Exception exc)
{
System.out.println("Errore - Driver jdbc non presente: "+
exc.getMessage());
}
try
{
Connection conn = DriverManager.getConnection(
"jdbc:mysql:///basedati",
"root",
"password"
);
BufferedReader keyb = new BufferedReader(
new InputStreamReader(System.in));
Statement stmt = conn.createStatement();
System.out.println("Inserire l'ID ");
String id = keyb.readLine();
System.out.println("Inserire il Cognome ");
String cognome = keyb.readLine();
System.out.println("Inserire il Nome ");
String nome = keyb.readLine();
System.out.println("Inserire l'età ");
String eta = keyb.readLine();
ResultSet rset = stmt.executeQuery("INSERT INTO dati(id, cognome, nome,
eta) VALUES ('"+id"', '"+cognome"', '"+nome"', '"+
eta+"')");
rset.close();
stmt.close();
conn.close();
}
catch (Exception exc)
{
System.out.println("Errore: "+ exc.getMessage());
}
}
}