Sunday, 11 April 2010

O aplicaţie simplă cu Java DB

import java.sql.*;

public class Student {

public static void main(String[] args) {

Connection dbConnection = null;     
String dbUrl =
"jdbc:derby:C:\\Sun\\SDK\\javadb\\bin\\student;"+
"user=profesor;password=parolaprofesor";

try{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
//incarca driver JDBC pt JavaDB
}

catch(ClassNotFoundException e) {
e.printStackTrace(); 
System.out.println("Eroare incarcare driver!\n" + e);
}

try{
dbConnection=DriverManager.getConnection(dbUrl);
Statement stmt= dbConnection.createStatement();
ResultSet  rs =
stmt.executeQuery(
" SELECT * FROM student");

while (rs.next()) {
System.out.println (
rs.getString ("nume") + "," +
rs.getString ("prenume") + "," +
rs.getString ("email"));
}
stmt.close();
}

catch(SQLException e) {
e.printStackTrace();
}
}
}
Programul se execută precizând în linia de comandă pentru maşina virtuală Java calea către driverul Java DB pentru SQL(derby.jar):

java -classpath .;C:\Sun\SDK\javadb\lib\derby.jar Student

No comments: