Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 dbtest;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- public class DBTest {
- // https://pastebin.com/raw/01RG6MGb
- // метод ля получения объекта типа Connection
- public static Connection getConn() throws Exception {
- String connStr = "jdbc:mariadb://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&user=root&password=";
- return DriverManager.getConnection(connStr);
- }
- //
- public static void test() throws Exception {
- // получаем соединение
- try (Connection c = getConn()) {
- // выполняем sql запрос: select * from t1 (выборка данных из таблицы t1 )
- PreparedStatement ps = c.prepareStatement("select id, name from t1");
- // выполняем вызов для получения данных
- ResultSet rs = ps.executeQuery();
- // в цикле получаем данные из таблицы
- while (rs.next()) {
- // извлекаем данные из определенных колонок таблицы
- String id = rs.getString(1); // порядковый номер колонки
- String name = rs.getString("name"); // "name"
- // выводим значения на экран
- System.out.println("id=" + id);
- System.out.println("name=" + name);
- }
- ps.close();
- }
- }
- //
- public static void main(String[] args) throws Exception {
- // TODO code application logic here
- test();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement