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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| package javajdbc1;
import java.sql.*;
public class Main {
public static String formatResultSet(ResultSet rs)
{
try
{
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
StringBuffer ret = new StringBuffer(500);
for (int i = 1; i <= numberOfColumns; i++)
{
String columnName = rsmd.getColumnName(i);
ret.append(columnName + "," );
}
ret.append("\n");
while (rs.next())
{
for (int i = 1; i <= numberOfColumns; i++)
ret.append(rs.getString(i) + "," );
ret.append("\n");
}
return(ret.toString());
}
catch(Exception e)
{
return e.toString();
}
}
public static void main(String args[])
{
String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
String URL = "jdbc:odbc:salarie";
boolean s_open=false, c_open=false;
Statement stmt = null;
Connection con = null;
String conString = null;
try
{
Class.forName(DRIVER);
con = DriverManager.getConnection(URL , "", "");
c_open = true;
stmt = con.createStatement();
s_open = true;
String strSQL="create table paie " +
"(NoSal integer , mois integer , SalaireMens integer , primary key (Nosal,mois) )";
System.out.println("\n>> CREATION TABLE Paie : "+strSQL );
stmt.executeUpdate(strSQL);
System.out.println("\n>> "+"Insertion de 6 paie pour janvier et fevrier ");
stmt.executeUpdate("insert into paie values(1,1,100)");
stmt.executeUpdate("insert into paie values(1,2,150)");
stmt.executeUpdate("insert into paie values(2,1,150)");
stmt.executeUpdate("insert into paie values(2,2,200)");
stmt.executeUpdate("insert into paie values(3,1,100)");
stmt.executeUpdate("insert into paie values(3,2,200)");
System.out.println("\n>> "+"Liste des Paies cumulées ");
ResultSet rs = stmt.executeQuery("select Nom, sum(salaireMens) as SalaireTotal from Paie p,Salarie s where s.Nosal=p.nosal group by Nom");
System.out.println(formatResultSet(rs));
System.out.println("\n>> "+"SUPPRESSION table Paie");
stmt.executeUpdate("drop table paie");
}
catch(Exception e)
{
System.out.println(e);
System.out.println("\nProbleme Acces JDBC ODBC\n");
}
try
{
if(s_open)
stmt.close();
}
catch(Exception e2)
{
System.out.println(e2.toString());
}
try
{
if(c_open)
con.close();
}
catch(Exception e3)
{
System.out.println(e3.toString());
}
}
} |
Partager