Created
January 7, 2025 06:51
-
-
Save borodicht/5a4d5af00990ed814effad814a013c2b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.*; | |
import java.util.Properties; | |
public class DBConnection { | |
private Connection connect = null; | |
private Statement statement = null; | |
private ResultSet resultSet = null; | |
private static String URL = "jdbc:mysql://localhost:3306/QA28"; | |
private static String USER = "root"; | |
private static String PASSWORD = "Password001!"; | |
public void connect() { | |
Properties properties = new Properties(); | |
properties.setProperty("user", USER); | |
properties.setProperty("password", PASSWORD); | |
properties.setProperty("allowPublicKeyRetrieval", "true"); | |
try { | |
connect = DriverManager.getConnection(URL, properties); | |
statement = connect.createStatement(); | |
System.out.println("Соеденение с БД выполнено"); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
public ResultSet select(String query) { | |
try { | |
return statement.executeQuery(query); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
public void insert(String query) { | |
try { | |
statement.execute(query); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void insertPrepared(String name) { | |
try { | |
PreparedStatement statement1 = connect.prepareStatement("INSERT INTO students(name) VALUES (?)"); | |
statement1.setString(1, name); | |
statement1.execute(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void delete(String query) { | |
try { | |
statement.execute(query); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void close() { | |
try { | |
if (connect != null) { | |
connect.close(); | |
System.out.println("Соеденение с БД закрыто"); | |
} | |
if (statement != null) { | |
statement.close(); | |
} | |
if (resultSet != null) { | |
resultSet.close(); | |
} | |
} catch (Exception ignored) { | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.testng.annotations.Test; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
public class DBTest { | |
@Test | |
public void checkDB() throws SQLException { | |
String name = "blabla'); DELETE FROM students;"; | |
DBConnection connection = new DBConnection(); | |
connection.connect(); | |
// connection.insert("INSERT INTO students (name) VALUES ('blabla')')"); | |
connection.insertPrepared("Borodich Timofei"); | |
ResultSet result = connection.select("SELECT * FROM students"); | |
while (result.next()) { | |
System.out.println(result.getInt("id")); | |
System.out.println(result.getString("name")); | |
System.out.println(result.getInt("group_id")); | |
} | |
connection.delete("DELETE FROM students WHERE name = 'Timofei Borodich'"); | |
connection.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment