Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getInstance()  RSS feed

 
nadia ghezaiel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have pb!!!After connxion BD to Sql server I wan't to write a class :to insert or modify inthe dataBase ,but infortunaly I should use thread!!!
That's why I use the method getInstance',but I don'tknow the implementation !!!it's the first time I use this method!!!
All my code!!!

Class Connexion:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Conn;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
*
* @author Nadia
*/
public class Connexion {


java.sql.Connection C;

private Connection connection;


/**
* Constructeur
*/
public Connexion() {
this.connection=ConnexionBDD.getInstance();
}

//*************Insertion Utilisateur

public int insertUtilisateur(UserIdentit utilisateur) {
int resultat = -1;
String requete = "INSERT INTO Authentif.dbo.tab_autnentif (Nomc,Adressec,MDP,VMDP)"+"VALUES(?,?,?,?)";
PreparedStatement pst;


try {
pst = C.prepareStatement(requete);
pst.setString(1,utilisateur.getNom());
pst.setString(2,utilisateur.getAdresseC());
pst.setString(3, utilisateur.getMDP());
pst.setString(4, utilisateur.getVMDP());

resultat = pst.executeUpdate();
pst.close();
pst = null;
} catch (SQLException e) {
System.out.println("Erreur lors de l'insertion de l'utilisateur.");
e.printStackTrace();
}

return resultat;
}
public static void main(String[] args) {

UserIdentit utilisat=new UserIdentit();
Connexion Ci=new Connexion();
Ci.insertUtilisateur(utilisat);

}
}

Class connexionBDD:


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Conn;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
*
* @author Nadia
*/
public class ConnexionBDD {

public static void main(String[] args) {

try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

}catch(ClassNotFoundException e){
System.out.println(e);
}
try{
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Authentif","sa","nadia");

}
catch(SQLException se){
System.out.println("SQL exception: " + se.getMessage());
}

}

static Connection getInstance() {
[color=green][size=24] ???

}[/size][/color]
}


and finally In Jframe!!!


private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:


Connexion c1=new Connexion();
String Nomc=Nom1.getText();
String AdresseC=Adresse1.getText();
char[] MDP=mdp1.getPassword();
char[] VMDP=vmdp2.getPassword();

}



Please help me, ((((



 
nadia ghezaiel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please what's the aim of the factory Singleton???what's the relation between getInstance() and Singleton???
 
Swastik Dey
Rancher
Posts: 1784
14
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getInstance() is the method that is returning the instance of the class. Singleton is design pattern this is typically used when you don't wish to create multiple instances of a class.

 
nadia ghezaiel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thinks
 
Swastik Dey
Rancher
Posts: 1784
14
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!