java.sql
& javax.sql
public class DBUtility {
public static String url = “jdbc:mysql://localhost:3306/jdbcdemos";
public static String username = "root";
public static String password = "root";
private static Connection connection = null;
public static Connection getDBConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver"); // No longer required from JDBC 4.0
Connection connection = DriverManager.getConnection (url, username, password);
return connection;
}
}
connection.close();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery();
PreparedStatement pstatement = connection.prepareStatement (retrievedata);
ResultSet resultSet = pstatement.executeQuery();
while(resultset.next()){}
?
in SQL query to pass parameterConnection connection = DBUtility.getDBConnection();
PreparedStatement pstatement = connection.prepareStatement(String);
pstatement.setInt(N, var);
int rowsInsertCount = pstatement.executeUpdate();
javax.persistence.Entity
javax.persistence.Id
@Entity
& @Id
@Table(name=””)
& @Column(name=””)
Temporal(TemporalType.DATE)
=> Used to convert to java.sql Date typepersistence.xml
by mapping with PersistenceUnitName
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("unit-name");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().commit();
EmployeeEntity employee = new EmployeeEntity();
employee.setEmployeeld(1);
employee.setEmployeeName("ABCDE");
employee.setInsertTime(new Date());
entityManager.getTransaction().begin();
entityManager.persist(employee);
Employee employee = em.find(Employee.class, idValue)
entityManage.remove(employeeEntity)
@GeneratedValue(strategy=GenerationType.AUTO)
entityManager.merge(employeeEntity);
=> Sync the changes done to the table in DB@OneToOne
JoinColumn(name=”colName”, unique=true)
private ClassName classEntity
@OneToOne(cascade=CascadeType.ALL)
@ManyToOne(cascade=CascadeType.ALL)
JoinColumn(name=”colName”, unique)
@OneToMany (cascade = CascadeType.ALL)
@JoinTable(name = "tableName", joinColumns = {@JoinColumn(name = "colName1 ") }, inverseJoinColumns = {@JoinColumn(name = “colName2”, unique = true) })
@ManyToMany (cascade = CascadeType.ALL)
@JoinTable(name = "tableName", joinColumns = {@JoinColumn(name = "colName1 ") }, inverseJoinColumns = {@JoinColumn(name = “colName2”) })
private List<NameEntity> names;
javax.persistence.Query
Query query = entityManager.createQuery("SELECT k FROM EmployeeEntity k");
List<EmployeeEntity> list = query.getResultList();
FROM EmployeeEntity
Query query = entityManager.createQuery("SELECT e.name FROM EmployeeEntity e");
Query query = entityManager.createQuery("SELECT e.name, e.id FROM EmployeeEntity e");
List<String> list = query.getResultList();
Query query = entityManager.createQuery("SELECT e FROM EmployeeEntity e WHERE e.salary>=?1 and e.salary<=?2");
query.setParameter(1, lowerbound);
query.setParameter(2, upperbound);
Query query = entityManager.createQuery("SELECT e FROM EmployeeEntity e WHERE e.salary>= :lb and e.salary<= :ub");
query.setParameter(lb, lowerbound);
query.setParameter(ub, upperbound);
java.sql
Query query = entityManager.createQuery("UPDATE EmployeeEntity e SET e.salary=?2 WHERE e.name= :ub");
query.setParameter(22, salary);
query.setParameter("ub", name);
entityManager.getTransaction().begin();
Integer res = query = executeUpdate();
entityManager.getTransaction().commit();
// Entity Class
@Entity
@Table(name = "Employee")
@NamedQuery(name="findAllEmployeeSalaryRangePositionalParam",
query="SELECT e FROM Employee Entity e where e.salary>=?1 and e.salary<=?2")
public class Employee Entity {}
@Entity
@Table(name = "Employee")
@NamedQueries({
@NamedQuery(name="findAllEmployeeSalaryRangePositionalParam",
query="SELECT e FROM Employee Entity e where e.salary>=?1 and e.salary<=?2"),
@NamedQuery(name="findAllEmployeeSalaryRangePositionalParam",
query="SELECT e FROM Employee Entity e where e.salary>=?1 and e.salary<=?2")
})
public class Employee Entity {}
// Employee DAOIMPL
Query query = entityManager.createNamedQuery("findAllEmployeeSalaryRangePositionalParam");
query.setParameter(1, lowerBound);
query.setParameter(2, upperBound);
List<EmployeeEntity> employeeList = query.getResultList();
<named-query name="">
<query></query>
</named-query>
Query query = entityManager.createQuery("select emp, ast from Employee Entity emp, AssetEntity ast where emp.assetId = ast.assetId");