Java programmation

b_sofyane Messages postés 2 Date d'inscription samedi 11 décembre 2004 Statut Membre Dernière intervention 10 juin 2005 - 10 juin 2005 à 14:06
cs_mep Messages postés 86 Date d'inscription vendredi 7 mai 2004 Statut Membre Dernière intervention 3 septembre 2008 - 10 juin 2005 à 14:53
jé un probleme dans ce programme jé pa trouvé le package jabadot
que dois je faire ?
package jabadot;
import java.sql.*;
import java.io.*;
import java.util.*;
/** A UserDB using JDBC and a relational DBMS.
* We use the inherited getUser ("Find the User object for a given
nickname")
* since we keep everything in memory in this version.
*/
public class UserDBJDBC extends UserDB {
protected final static String DB_URL = JDConstants.getProperty("jabadot.userdb.url");
protected PreparedStatement setPasswordStatement;
protected PreparedStatement addUserStmt;
protected PreparedStatement setLastLoginStmt;
protected PreparedStatement deleteUserStmt;
/** Default constructor */
protected UserDBJDBC( )throws ClassNotFoundException, SQLException, IOException {
this(DB_URL);
}
/** Constructor */
public UserDBJDBC(String fn)
throws ClassNotFoundException, SQLException, IOException {
super( );
// Load the database driver
Class.forName("jdbc.idbDriver");
Connection conn = DriverManager.getConnection(fn,"www", ""); // user, password
Statement stmt = conn.createStatement( );
ResultSet rs = stmt.executeQuery("select * from userdb");
while (rs.next( )) {
//name:password:fullname:City:Prov:Country:privs
// Get the fields from the query.
String nick = rs.getString(1);
String pass = rs.getString(2);
String full = rs.getString(3);
String email = rs.getString(4);
String city = rs.getString(5);
String prov = rs.getString(6);
String ctry = rs.getString(7);
int iprivs = rs.getInt(8);
// Construct a user object from the fields
User u = new User(nick, pass, full, email,city, prov, ctry, iprivs);
// Add it to the in-memory copy.
users.add(u);
}
stmt.close( );
rs.close( ); // All done with that resultset
// Set up the PreparedStatements now so we don't have to
// re-create them each time needed.
addUserStmt = conn.prepareStatement("insert into userdb values (?,?,?,?,?,?,?,?)");setPasswordStatement conn.prepareStatement("update userdb SET password ? where name = ?");setLastLoginStmt conn.prepareStatement("update userdb SET lastLogin ? where name = ?");deleteUserStmt conn.prepareStatement("delete from userdb where name ?");
}
/** Add one user to the list, both in-memory and on disk. */
public synchronized void addUser(User nu)
throws IOException, SQLException {
// Add it to the in-memory list
super.addUser(nu);
// Copy fields from user to DB
addUserStmt.setString(1, nu.name);
addUserStmt.setString(2, nu.password);
addUserStmt.setString(3, nu.fullName);
addUserStmt.setString(4, nu.email);
addUserStmt.setString(5, nu.city);
addUserStmt.setString(6, nu.prov);
addUserStmt.setString(7, nu.country);
addUserStmt.setInt (8, nu.getPrivs( ));
// Store in persistent DB
addUserStmt.executeUpdate( );
}
public void deleteUser(String nick) throws SQLException {
// Find the user object
User u = getUser(nick);
if (u == null) {
throw new SQLException("User " + nick + " not in in-memoryDB");
}
deleteUserStmt.setString(1, nick);
int n = deleteUserStmt.executeUpdate( );
if (n != 1) { // not just one row??
/*CANTHAPPEN */
throw new SQLException("ERROR: deleted " + n + " rows!!");
}
// IFF we deleted it from the DB, also remove from the inmemory
list
users.remove(u);
}
public synchronized void setPassword(String nick, String newPass)
throws SQLException {
// Find the user object
User u = getUser(nick);
// Change it in DB first; if this fails, the info in
// the in-memory copy won't be changed either.
setPasswordStatement.setString(1, newPass);
setPasswordStatement.setString(2, nick);
setPasswordStatement.executeUpdate( );
// Change it in-memory
u.setPassword(newPass);
}
/** Update the Last Login Date field. */
public synchronized void setLoginDate(String nick, java.util.Date date)
throws SQLException {
// Find the user object
User u = getUser(nick);
// Change it in DB first; if this fails, the date in
// the in-memory copy won't be changed either.
// Have to convert from java.util.Date to java.sql.Date here.
// Would be more efficient to use java.sql.Date everywhere.
setLastLoginStmt.setDate(1, new java.sql.Date(date.getTime()));
setLastLoginStmt.setString(2, nick);
setLastLoginStmt.executeUpdate( );
// Change it in-memory
u.setLastLoginDate(date);
}
}///:~

1 réponse

cs_mep Messages postés 86 Date d'inscription vendredi 7 mai 2004 Statut Membre Dernière intervention 3 septembre 2008 4
10 juin 2005 à 14:53
la ligne package jabadot; définit le paquet dans lequel tu met ton programme. Si tu na pas de paquete enleve simplement la ligne...
0
Rejoignez-nous