Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.ArrayList;
- import java.sql.Connection;
- public class DatabaseConnection {
- private Connection connection;
- public DatabaseConnection() throws Exception {
- // TODO Auto-generated constructor stub
- getConnection();
- }
- private Connection getConnection() throws Exception{
- try{
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost:3306/fibbo";
- String username = "root";
- String password = "";
- Class.forName(driver);
- Connection conn = DriverManager.getConnection(url,username,password);
- System.out.println("Connected");
- this.connection = conn;
- } catch(Exception e){System.out.println(e);} return null;
- }
- public void getNumbers() {
- try {
- PreparedStatement get = connection.prepareStatement("SELECT * FROM fibbo");
- ResultSet result = get.executeQuery();
- ArrayList<String> arrayList = new ArrayList<>();
- while(result.next()) {
- arrayList.add(result.getString("id") + ": " + result.getString("number"));
- }
- }catch (Exception e) {
- System.out.println(e);
- }
- }
- public void getNumber(int index) {
- try {
- PreparedStatement get = connection.prepareStatement("SELECT * FROM fibbo WHERE id='"+index+"'");
- get.executeQuery();
- }catch (Exception e) {
- System.out.println(e);
- }
- }
- public void insertNumber(int index, int number) {
- try {
- PreparedStatement insert = connection.prepareStatement("INSERT INTO fibbo (id, number) VALUES ('" + index + "', '" + number +"')");
- insert.executeUpdate();
- }catch (Exception e) {
- System.out.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement