Created
September 4, 2019 07:05
-
-
Save TeijiW/b486b5c59d4d66e47e6951871a47ded6 to your computer and use it in GitHub Desktop.
A search in a Database
This file contains 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
-- MySQL dump 10.13 Distrib 5.7.27, for Linux (x86_64) | |
-- | |
-- Host: localhost Database: ticket_gate | |
-- ------------------------------------------------------ | |
-- Server version 5.7.27-0ubuntu0.18.04.1 | |
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | |
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | |
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | |
/*!40101 SET NAMES utf8 */; | |
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; | |
/*!40103 SET TIME_ZONE='+00:00' */; | |
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; | |
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; | |
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; | |
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; | |
-- | |
-- Table structure for table `students` | |
-- | |
DROP TABLE IF EXISTS `students`; | |
/*!40101 SET @saved_cs_client = @@character_set_client */; | |
/*!40101 SET character_set_client = utf8 */; | |
CREATE TABLE `students` ( | |
`StudentID` varchar(20) COLLATE utf8_bin NOT NULL, | |
`StudentName` varchar(255) COLLATE utf8_bin NOT NULL, | |
`EntryTime` time NOT NULL, | |
`Departament` varchar(255) COLLATE utf8_bin NOT NULL, | |
PRIMARY KEY (`StudentID`), | |
UNIQUE KEY `StudentID` (`StudentID`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; | |
/*!40101 SET character_set_client = @saved_cs_client */; | |
-- | |
-- Dumping data for table `students` | |
-- | |
LOCK TABLES `students` WRITE; | |
/*!40000 ALTER TABLE `students` DISABLE KEYS */; | |
INSERT INTO `students` VALUES ('01047595184','Victória da Silva','09:40:00','DAI'),('07764975109','Alessandro Teiji','07:06:52','DAI'),('19426933','Thamires Martins','18:20:00','DEAEE'),('28677935','Vitória Lopes','10:30:00','DACC'),('29680913','Mayan Luiza','12:45:00','DAI'); | |
/*!40000 ALTER TABLE `students` ENABLE KEYS */; | |
UNLOCK TABLES; | |
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; | |
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; | |
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; | |
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; | |
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; | |
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; | |
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; | |
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; | |
-- Dump completed on 2019-09-04 3:04:13 |
This file contains 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
package ticketgate; | |
import java.sql.Connection; | |
import java.sql.Statement; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.ResultSetMetaData; | |
import java.sql.SQLException; | |
import javax.swing.JOptionPane; | |
public class TicketGate { | |
static final String DATABASE_URL = "jdbc:mysql://localhost/ticket_gate?serverTimezone=UTC"; | |
public static void main(String args[]) { | |
Connection connection = null; | |
Statement statement = null; | |
ResultSet resultSet = null; | |
String searchTerm = JOptionPane.showInputDialog("Digite o termo para pesquisa"); | |
searchTerm = searchTerm.substring(0, 1).toUpperCase() + searchTerm.substring(1); | |
try { | |
connection = DriverManager.getConnection(DATABASE_URL, "root", ""); | |
statement = connection.createStatement(); | |
resultSet = statement.executeQuery("" | |
+ "SELECT " | |
+ "StudentID, StudentName, EntryTime, Departament " | |
+ "FROM " | |
+ "students " | |
+ "WHERE StudentName LIKE '%" + searchTerm+"%'"); | |
ResultSetMetaData metaData = resultSet.getMetaData(); | |
int numberOfColumns = metaData.getColumnCount(); | |
System.out.println("Ticket Gate Schedules \n"); | |
for (int i = 1; i <= numberOfColumns; i++) { | |
System.out.printf("%-8s\t", metaData.getColumnName(i)); | |
} | |
System.out.println(); | |
while (resultSet.next()) { | |
for (int i = 1; i <= numberOfColumns; i++) { | |
System.out.printf("%-8s\t", resultSet.getObject(i)); | |
} | |
} | |
} catch (SQLException sqlException) { | |
sqlException.printStackTrace(); | |
} finally { | |
try { | |
resultSet.close(); | |
statement.close(); | |
connection.close(); | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment