Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.sql.ResultSet;
- public class Handler {
- private String address="";
- private String username="";
- private String password="";
- private String databaseName="";
- public Handler(String address, String username, String password, String databaseName) {
- this.address = address;
- this.username = username;
- this.password = password;
- this.databaseName = databaseName;
- }
- public Handler(String address, String username, String password) {
- this.address = address;
- this.username = username;
- this.password = password;
- }
- public String getAddress(){
- return address;
- }
- public String getUsername() {
- return username;
- }
- public String getPassword() {
- return password;
- }
- public String getDatabaseName() {
- return databaseName;
- }
- public static Connection conn = null;
- public void connect(){
- String uri = "";
- uri = "jdbc:mysql://"+this.getAddress()+"/"+this.getDatabaseName()+"?user="+this.getUsername()+"&password="+this.getPassword();
- try {
- conn = DriverManager.getConnection(uri);
- }catch(SQLException ex ){
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- }
- }
- public static void main(String[] args){
- Statement stmt = null;
- ResultSet rs = null;
- //Get a record
- try{
- stmt=conn.createStatement();
- if (stmt.execute("SELECT * FROM Paziente ")) {
- rs = stmt.getResultSet();
- conn.close();
- }
- }catch (SQLException ex){
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- }
- //Insert a record
- try{
- stmt=conn.createStatement();
- stmt.executeUpdate("INSERT INTO Reparto " + "VALUES (Chirurgia)");
- conn.close();
- }catch (SQLException ex){
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- }
- }
- }
Add Comment
Please, Sign In to add comment