mirror of
https://github.com/guilhermewerner/java-sql
synced 2025-06-14 20:24:19 +00:00
Create eclipse project
This commit is contained in:
44
.classpath
Normal file
44
.classpath
Normal file
@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="test" value="true"/>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="target/generated-sources/annotations">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="ignore_optional_problems" value="true"/>
|
||||
<attribute name="m2e-apt" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="ignore_optional_problems" value="true"/>
|
||||
<attribute name="m2e-apt" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
34
.project
Normal file
34
.project
Normal file
@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>01ConexaoPostgres</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
<filteredResources>
|
||||
<filter>
|
||||
<id>1630783759058</id>
|
||||
<name></name>
|
||||
<type>30</type>
|
||||
<matcher>
|
||||
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||
</matcher>
|
||||
</filter>
|
||||
</filteredResources>
|
||||
</projectDescription>
|
15
pom.xml
Normal file
15
pom.xml
Normal file
@ -0,0 +1,15 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.ti2cc</groupId>
|
||||
<artifactId>01ConexaoPostgres</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>42.2.16</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
158
src/main/java/com/ti2cc/DAO.java
Normal file
158
src/main/java/com/ti2cc/DAO.java
Normal file
@ -0,0 +1,158 @@
|
||||
package com.ti2cc;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
public class DAO {
|
||||
private Connection conexao;
|
||||
|
||||
public DAO() {
|
||||
conexao = null;
|
||||
}
|
||||
|
||||
public boolean conectar() {
|
||||
String driverName = "org.postgresql.Driver";
|
||||
|
||||
String server = "localhost";
|
||||
int porta = 5432;
|
||||
|
||||
String username = "postgres";
|
||||
String password = "1234";
|
||||
|
||||
String mydatabase = "postgres";
|
||||
|
||||
boolean status = false;
|
||||
|
||||
String url = "jdbc:postgresql://" + server + ":" + porta + "/" + mydatabase;
|
||||
|
||||
try {
|
||||
Class.forName(driverName);
|
||||
conexao = DriverManager.getConnection(url, username, password);
|
||||
status = (conexao == null);
|
||||
System.out.println("Conexão efetuada com o postgres!");
|
||||
} catch (ClassNotFoundException e) {
|
||||
System.err.println("Conexão NÃO efetuada com o postgres -- Driver não encontrado -- " + e.getMessage());
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Conexão NÃO efetuada com o postgres -- " + e.getMessage());
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
public boolean close() {
|
||||
boolean status = false;
|
||||
|
||||
try {
|
||||
conexao.close();
|
||||
status = true;
|
||||
} catch (SQLException e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
public boolean inserirUsuario(Usuario usuario) {
|
||||
boolean status = false;
|
||||
try {
|
||||
Statement st = conexao.createStatement();
|
||||
st.executeUpdate("INSERT INTO usuario (codigo, login, senha, sexo) " + "VALUES (" + usuario.getCodigo()
|
||||
+ ", '" + usuario.getLogin() + "', '" + usuario.getSenha() + "', '" + usuario.getSexo() + "');");
|
||||
st.close();
|
||||
status = true;
|
||||
} catch (SQLException u) {
|
||||
throw new RuntimeException(u);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
public boolean atualizarUsuario(Usuario usuario) {
|
||||
boolean status = false;
|
||||
try {
|
||||
Statement st = conexao.createStatement();
|
||||
String sql = "UPDATE usuario SET login = '" + usuario.getLogin() + "', senha = '" + usuario.getSenha()
|
||||
+ "', sexo = '" + usuario.getSexo() + "'" + " WHERE codigo = " + usuario.getCodigo();
|
||||
st.executeUpdate(sql);
|
||||
st.close();
|
||||
status = true;
|
||||
} catch (SQLException u) {
|
||||
throw new RuntimeException(u);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
public boolean excluirUsuario(int codigo) {
|
||||
boolean status = false;
|
||||
try {
|
||||
Statement st = conexao.createStatement();
|
||||
st.executeUpdate("DELETE FROM usuario WHERE codigo = " + codigo);
|
||||
st.close();
|
||||
status = true;
|
||||
} catch (SQLException u) {
|
||||
throw new RuntimeException(u);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
public Usuario[] getUsuarios() {
|
||||
Usuario[] usuarios = null;
|
||||
|
||||
try {
|
||||
Statement st = conexao.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
|
||||
ResultSet rs = st.executeQuery("SELECT * FROM usuario");
|
||||
if (rs.next()) {
|
||||
rs.last();
|
||||
usuarios = new Usuario[rs.getRow()];
|
||||
rs.beforeFirst();
|
||||
|
||||
for (int i = 0; rs.next(); i++) {
|
||||
usuarios[i] = new Usuario(rs.getInt("codigo"), rs.getString("login"), rs.getString("senha"),
|
||||
rs.getString("sexo").charAt(0));
|
||||
}
|
||||
}
|
||||
st.close();
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
return usuarios;
|
||||
}
|
||||
|
||||
public Usuario getUsuario(int codigo) {
|
||||
Usuario usuario = null;
|
||||
|
||||
try {
|
||||
Statement st = conexao.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
|
||||
ResultSet rs = st.executeQuery("SELECT * FROM usuario WHERE usuario.codigo = " + codigo);
|
||||
if (rs.first()) {
|
||||
usuario = new Usuario(rs.getInt("codigo"), rs.getString("login"), rs.getString("senha"),
|
||||
rs.getString("sexo").charAt(0));
|
||||
}
|
||||
st.close();
|
||||
} catch (SQLException u) {
|
||||
throw new RuntimeException(u);
|
||||
}
|
||||
|
||||
return usuario;
|
||||
}
|
||||
|
||||
public Usuario[] getUsuariosMasculinos() {
|
||||
Usuario[] usuarios = null;
|
||||
|
||||
try {
|
||||
Statement st = conexao.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
|
||||
ResultSet rs = st.executeQuery("SELECT * FROM usuario WHERE usuario.sexo LIKE 'M'");
|
||||
if (rs.next()) {
|
||||
rs.last();
|
||||
usuarios = new Usuario[rs.getRow()];
|
||||
rs.beforeFirst();
|
||||
|
||||
for (int i = 0; rs.next(); i++) {
|
||||
usuarios[i] = new Usuario(rs.getInt("codigo"), rs.getString("login"), rs.getString("senha"),
|
||||
rs.getString("sexo").charAt(0));
|
||||
}
|
||||
}
|
||||
st.close();
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
return usuarios;
|
||||
}
|
||||
}
|
82
src/main/java/com/ti2cc/Principal.java
Normal file
82
src/main/java/com/ti2cc/Principal.java
Normal file
@ -0,0 +1,82 @@
|
||||
package com.ti2cc;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Principal {
|
||||
public static void main(String[] args) {
|
||||
DAO dao = new DAO();
|
||||
dao.conectar();
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
boolean quit = false;
|
||||
|
||||
do {
|
||||
System.out.println("[1] Listar");
|
||||
System.out.println("[2] Inserir");
|
||||
System.out.println("[3] Excluir");
|
||||
System.out.println("[4] Atualizar");
|
||||
System.out.println();
|
||||
System.out.println("[0] Sair");
|
||||
|
||||
int option = scanner.nextInt();
|
||||
|
||||
if (option == 1) { // Listar
|
||||
Usuario[] usuarios = dao.getUsuarios();
|
||||
|
||||
for (int i = 0; i < usuarios.length; i++) {
|
||||
System.out.println(usuarios[i].toString());
|
||||
}
|
||||
} else if (option == 2) { // Inserir
|
||||
System.out.print("Informe o código do usuário: ");
|
||||
int codigo = scanner.nextInt();
|
||||
|
||||
System.out.print("Informe o login do usuário: ");
|
||||
String login = scanner.next();
|
||||
|
||||
System.out.print("Informe a senha do usuário: ");
|
||||
String senha = scanner.next();
|
||||
|
||||
System.out.print("Informe o sexo do usuário: ");
|
||||
char sexo = scanner.next().charAt(0);
|
||||
|
||||
Usuario usuario = new Usuario(codigo, login, senha, sexo);
|
||||
|
||||
if (dao.inserirUsuario(usuario)) {
|
||||
System.out.println("Usuário " + codigo + " inserido!");
|
||||
}
|
||||
} else if (option == 3) { // Excluir
|
||||
System.out.print("Informe o código do usuário: ");
|
||||
int codigo = scanner.nextInt();
|
||||
|
||||
if (dao.excluirUsuario(codigo)) {
|
||||
System.out.println("Usuário " + codigo + " excluído!");
|
||||
}
|
||||
} else if (option == 4) { // Atualizar
|
||||
System.out.print("Informe o código do usuário: ");
|
||||
int codigo = scanner.nextInt();
|
||||
|
||||
Usuario usuario = dao.getUsuario(codigo);
|
||||
|
||||
System.out.print("Informe o login do usuário: ");
|
||||
usuario.setLogin(scanner.next());
|
||||
|
||||
System.out.print("Informe a senha do usuário: ");
|
||||
usuario.setSenha(scanner.next());
|
||||
|
||||
System.out.print("Informe o sexo do usuário: ");
|
||||
usuario.setSexo(scanner.next().charAt(0));
|
||||
|
||||
if (dao.atualizarUsuario(usuario)) {
|
||||
System.out.println("Usuário " + codigo + " atualizado!");
|
||||
}
|
||||
} else if (option == 0) { // Sair
|
||||
quit = true;
|
||||
} else {
|
||||
System.out.println("Opção inválida!");
|
||||
}
|
||||
} while (!quit);
|
||||
|
||||
scanner.close();
|
||||
dao.close();
|
||||
}
|
||||
}
|
59
src/main/java/com/ti2cc/Usuario.java
Normal file
59
src/main/java/com/ti2cc/Usuario.java
Normal file
@ -0,0 +1,59 @@
|
||||
package com.ti2cc;
|
||||
|
||||
public class Usuario {
|
||||
private int codigo;
|
||||
private String login;
|
||||
private String senha;
|
||||
private char sexo;
|
||||
|
||||
public Usuario() {
|
||||
this.codigo = -1;
|
||||
this.login = "";
|
||||
this.senha = "";
|
||||
this.sexo = '*';
|
||||
}
|
||||
|
||||
public Usuario(int codigo, String login, String senha, char sexo) {
|
||||
this.codigo = codigo;
|
||||
this.login = login;
|
||||
this.senha = senha;
|
||||
this.sexo = sexo;
|
||||
}
|
||||
|
||||
public int getCodigo() {
|
||||
return codigo;
|
||||
}
|
||||
|
||||
public void setCodigo(int codigo) {
|
||||
this.codigo = codigo;
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public void setLogin(String login) {
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
public String getSenha() {
|
||||
return senha;
|
||||
}
|
||||
|
||||
public void setSenha(String senha) {
|
||||
this.senha = senha;
|
||||
}
|
||||
|
||||
public char getSexo() {
|
||||
return sexo;
|
||||
}
|
||||
|
||||
public void setSexo(char sexo) {
|
||||
this.sexo = sexo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Usuario { codigo: " + codigo + ", login: \"" + login + "\", senha: \"" + senha + "\", sexo: \"" + sexo + "\" }";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user