List roles;
SelectQuery select_role = new SelectQuery(Role.class);
roles = Context.getContext().performQuery(select_role);
Calendar c = new GregorianCalendar();
c.set(c.get(Calendar.YEAR) - 100, 0, 1, 0, 0, 0);
Expression qualifier3 = Expression.fromString("artist.dateOfBirth < $date");
qualifier3 = qualifier3.expWithParameters(Collections.singletonMap("date", c.getTime()));
SelectQuery select3 = new SelectQuery(Painting.class, qualifier3);
List paintings3 = context.performQuery(select3);
наверное добавить лисенер на саму форму окна приложения, тогда событие будет обрабатываться во всех случаях, когда окно приложения активно.
frame.addKeyListener(new MyKey());
Snake s = new Snake();
package application;
public class Application {
public static Snake s;
public static void main(String[] args) {
s = new Snake();
s.go();
}
}
package application;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import javax.swing.*;
import java.awt.event.KeyEvent;
public class Snake extends JComponent {
int x = 350;
int y = 350;
public MyPan drawPanel;
JFrame frame;
Timer timer = new Timer(100, new MyKey());
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPanel = new MyPan(this);
timer.start();
frame.getContentPane().add(drawPanel);
frame.setSize(700, 700);
frame.setVisible(true);
frame.setFocusable(true);
frame.setFocusTraversalKeysEnabled(false);
frame.addKeyListener(new MyKey());
frame.setFocusable(true);
}
public void moveRight() {
x += 10;
try {
Thread.sleep(20);
} catch (Exception e) {
e.printStackTrace();
}
}
public void moveLeft() {
x -= 10;
try {
Thread.sleep(20);
} catch (Exception e) {
e.printStackTrace();
}
}
public void moveDown() {
y += 10;
try {
Thread.sleep(20);
} catch (Exception e) {
e.printStackTrace();
}
}
public void moveUp() {
y -= 10;
try {
Thread.sleep(20);
} catch (Exception e) {
e.printStackTrace();
}
}
public class MyKey extends KeyAdapter implements ActionListener {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
moveRight();
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
moveUp();
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
moveLeft();
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
moveDown();
}
}
@Override
public void actionPerformed(ActionEvent e) {
drawPanel.repaint();
}
}
}
package application;
import javax.swing.*;
import java.awt.*;
public class MyPan extends JPanel {
// Snake snake = new Snake();
private Snake s;
public MyPan(Snake ss) {
s = ss;
}
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 700, 700);
g.setColor(Color.GREEN);
g.fillRoundRect(s.x, s.y, 50, 50, 40, 40);
}
}
dCaps.setCapability("takesScreenshot", false);
ChromeOptions options = new ChromeOptions();
options.setBinary(<путь до бинарника>);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
//откл скриншоты при Exception
capabilities.setCapability("takesScreenshot", false);
WebDriver driver = new ChromeDriver(capabilities);
// и я еще использую библу от "селенид"
WebDriverRunner.setWebDriver(driver);
public static List<Order> getOrders() throws SQLException, ... {
List<Order> result = new ArrayList<>();
InitialContext initContext = new InitialContext();
DataSource ds = (DataSource) initContext.lookup("jdbc/ИМЯ_РЕСУРСА");
try (Connection conn = ds.getConnection()) {
Statement stm = conn.createStatement();
ResultSet res = stm.executeQuery(" SELECT * FROM `orders` бла-бла-бла ;");
//кешируем запрос
CachedRowSet crs = new com.sun.rowset.CachedRowSetImpl();
crs.populate(res);
//работаем с ним
if (crs.size() > 0) {
crs.first();
do {
String id = crs.getString("id");
String number = crs.getString("number");
//формирование результ.Объекта
result.add(new Order(id, number));
} while (crs.next());
countRows = result.size(); //сохраним колво
}
} //try
return result;
}
После ресурсы подключения к БД автоматически закрываются.