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 model;
- /**
- *
- * @author ticu
- */
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class SQLDatabaseConnection {
- private String connectionUrl = "jdbc:sqlserver://localhost;user=SA;password=Password123;database=MagicApp;";
- private Statement stmt;
- // Connect to your database.
- // Replace server name, username, and password with your credentials
- public SQLDatabaseConnection(int ID,String name){
- try (Connection con = DriverManager.getConnection(connectionUrl); Statement stmt = con.createStatement();) {
- String SQL = "SELECT TOP 10 * FROM Users";
- String sql2 = "INSERT INTO Users VALUES ("+ID+", '"+name+"', 'Ionut', 'USA', 'email@email.dk')";
- stmt.executeUpdate(sql2);// sql to insert values
- ResultSet rs = stmt.executeQuery(SQL); // retrieve a result set which we can use to iterate through it after.
- // Iterate through the data in the result set and display it.
- while (rs.next()) {
- System.out.println(rs.getString("Name") + " " + rs.getString("SecondName")); //Name and SecondName are fields that actualy exist in the database
- }
- } // Handle any errors that may have occurred.
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement