1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
   |  
import java.sql.*;
import java.util.*;
 
public class sql {
 
    public static void main(String args[]) {
	String host = "localhost";
	String base = "test";
	String user = "pampa";
	String adresse = "adresse";
	try { 
	    host = args[0]; 
	    base = args[1];
	    user = args[2];
	    adresse = args[3]; 
	} catch (Exception e) {}
	System.err.println("Opening JDBC connection");
	System.err.println("host = " + host);
	System.err.println("base = " + base);
	System.err.println("user = " + user);
	System.err.println("adresse = " + adresse);
	String driver = "com.mysql.jdbc.Driver";
	String URL = "jdbc:mysql://" + host + "/" + 
	    base;
	if (!user.equals(""))
	    URL = URL + "?user=" + user;
	Connection c = null;
	Statement instr = null;
 
	try {
	    Class.forName(driver);
	} catch (Exception e) { 
	    System.err.println("driver load failed : " + e.getMessage()); 
	    System.exit(1);
	}
	try {
	for ( Enumeration e=DriverManager.getDrivers(); e.hasMoreElements();){
		System.out.println("Drivers "+e.nextElement().getClass().getName());
		};
	    c = DriverManager.getConnection(URL, user, "");
	    instr = c.createStatement();
	} catch (Exception e) {
	    System.err.println("connection failed " + e.getMessage());
	    if (c != null) try { c.close(); } catch (Exception ee) {}
	    System.exit(2);
	}
	try {
	    instr.executeUpdate("DROP TABLE if exists PAMPA_tableTest");
	} catch (Exception e) {}
	try {
	    instr.executeUpdate("CREATE TABLE PAMPA_tableTest (" +
				"NOM VARCHAR (20) NOT NULL, " +
				"NUM INT NOT NULL, " +
				"ADRESSE VARCHAR (50) NOT NULL, " +
				"PRIMARY KEY (NOM)" +
				")" );
	    instr.executeUpdate("INSERT INTO PAMPA_tableTest VALUES (\"polo_le_routier\", 123 , \"8 rue du RAT\");");
	    instr.executeUpdate("INSERT INTO PAMPA_tableTest VALUES (\"janine\", 456, \"20 rue du chat\");");
	    instr.executeUpdate("INSERT INTO PAMPA_tableTest VALUES (\"pampa\", 302, \" 9 RUE DES POIRIERS REAUX\");");
	    ResultSet result=instr.executeQuery("Select * From PAMPA_tableTest");
	    while (result.next()){                                        
		String NOM_Test = result.getString("NOM");
		int  NUM_Test = result.getInt(2);
		String ADRESSE_Test = result.getString("ADRESSE");
 
		System.out.println("NOM = "+ NOM_Test);
		System.out.println("\tNUM  = "+ NUM_Test);
		System.out.println("ADRESSE = "+ ADRESSE_Test);
		}
	    instr.close();
	    c.close();
	} catch (Exception e) { e.printStackTrace(); }
    }
} | 
Partager