Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.sql.*;
- /**
- * Created by BUHARIN on 19.08.2016.
- * <dependency>
- <groupId>com.h2database</groupId>
- <artifactId>h2</artifactId>
- <version>1.4.190</version>
- </dependency>
- */
- public class Solution {
- private Connection conn;
- private Statement stat;
- public JFrame window;
- public JPanel panel;
- public JButton knopochka;
- public JTextField text;
- public JLabel vivod;
- public Solution() throws SQLException, ClassNotFoundException {
- Class.forName("org.postgresql.Driver");
- conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres","postgres", "1");
- // conn = DriverManager.getConnection("jdbc:h2:./h2db", "sa", "");
- stat.execute("drop table if exists Table1");
- stat.execute("create table if not exists Table1(id int auto_increment, name varchar(255),email varchar(255),PhoneNumber integer, primary key (id))");
- stat.executeUpdate("insert into Table1 (name,email,PhoneNumber) values('kurlik','petusharnya@kurly.pok','22499')");
- stat.executeUpdate("insert into Table1 (name,email,PhoneNumber) values('makim','xolod@xolod.xolod','010101')");
- stat.executeUpdate("insert into Table1 (name,email,PhoneNumber) values('roberto','petusharnya_malaya@kurly.pok','22498')");
- window=new JFrame();
- //window.getContentPane().setContentPane(null);
- window.setSize(300,300);
- window.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- try {
- stat.close();
- conn.close();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- System.exit(0);
- }
- });
- panel=new JPanel();
- panel.setLayout(new BorderLayout());
- knopochka=new JButton("Like Gagarin");
- knopochka.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- search(text.getText());
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- });
- panel.add(knopochka, BorderLayout.NORTH);
- window.add(panel);
- vivod = new JLabel();
- text = new JTextField();
- panel.add(vivod, BorderLayout.CENTER);
- panel.add(text, BorderLayout.SOUTH);
- window.setVisible(true);
- }
- public void search (String stroka) throws SQLException {
- ResultSet rs;
- rs = stat.executeQuery("select * from Table1 where name='"+ stroka +"'");
- String z;
- while (rs.next()) {
- System.out.println(rs.getString("name"));
- System.out.println(rs.getString("email"));
- System.out.println(rs.getString("PhoneNumber"));
- z = rs.getString("name")+" "+rs.getString("email")+" "+rs.getString("PhoneNumber");
- showResult(z);
- }
- }
- public void showResult (String s){
- vivod.setText(s);
- }
- public static void main(String[] args) throws SQLException, ClassNotFoundException {
- Solution sol = new Solution();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement