You are on page 1of 5

/*

* To change this license header, choose License Headers in Project


Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package exemple;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

/**

* @author vali

*/

class javabean{

String error;

Connection con;

// Conectarea la baza de date

public void connect() throws ClassNotFoundException, SQLException,


Exception{

try

Class.forName("com.mysql.jdbc.Driver");
con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/test3?
useSSL=false","root", "parola");

catch (ClassNotFoundException cnfe)

error = "ClassNotFoundException: Nu s-a gasit driverul


bazei de date.";

throw new ClassNotFoundException(error);

catch (SQLException cnfe)

error = "SQLException: Nu se poate conecta la baza de


date.";

throw new SQLException(error);

catch (Exception e)

error = "Exception: A aparut o exceptie neprevazuta in


timp ce se stabilea legatura la baza de date.";

throw new Exception(error);

// end connect()

public ResultSet vedeTabel(String tabel) throws SQLException,


Exception{

ResultSet rs = null;

try
{

String queryString = ("select * from `test3`.`" + tabel +


"`;");

Statement stmt = con.createStatement();

rs = stmt.executeQuery(queryString);

catch (SQLException sqle)

error = "SQLException: Interogarea nu a fost posibila.";

throw new SQLException(error);

catch (Exception e)

error = "A aparut o exceptie in timp ce se extrageau


datele.";

throw new Exception(error);

return rs;

// end vedeTabel()

public void disconnect() throws SQLException

try

if ( con != null )

{
con.close();

catch (SQLException sqle)

error = ("SQLException: Nu se poate inchide conexiunea


la baza de date.");

throw new SQLException(error);

} // disconnect()

public class Curs3_0 {

/**

* @param args the command line arguments

*/

public static void main(String[] args) throws SQLException, Exception {

// TODO code application logic here

try {

javabean ob = new javabean();

ob.connect();

ResultSet rs = ob.vedeTabel("Clienti");

// afisarea datelor

afisare(rs);

ob.disconnect();
}catch(SQLException e){

System.out.println("Eroare SQL" + e.toString());

}catch(Exception e){

System.out.println("Eroare" + e.toString());

public static void afisare(ResultSet rs) throws ClassNotFoundException,


SQLException, Exception{

int idClient, idDepartament, Salariu;

String Nume, Prenume, Functie;

while(rs.next()){

idClient = rs.getInt("idClient");

Nume = rs.getString("Nume");

Prenume = rs.getString("Prenume");

Salariu = rs.getInt("Salariu");

Functie = rs.getString("Functie");

idDepartament = rs.getInt("idDepartament");

System.out.println("idClient = " + idClient + ", Nume = "


+ Nume + ", Prenume = " + Prenume + ", Salariu = " + Salariu + ", Functie =
" + Functie + ", idDepartament = " + idDepartament);

You might also like