Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package projetoexemplosgbd;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class Jdbc {
- public static void main(String[] args) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost/Sam";
- String user = "root";
- String pass = "hdt";
- Connection con = DriverManager.getConnection(url, user, pass);
- // a partir daqui você precisa pensar na lógica
- Scanner in = new Scanner(System.in);
- if (in.nextInt() == 0) {
- String sql = "select * from Exemplo";
- ResultSet rs = con.createStatement().executeQuery(sql);
- while (rs.next()) {
- System.out.println("valor de bar: " + rs.getBoolean("bar"));
- }
- } else {
- int id = in.nextInt();
- String f = in.next();
- boolean b = in.nextBoolean();
- String sql = "insert into Exemplo values (?, ?, ?)";
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setInt(1, id);
- ps.setString(2, f);
- ps.setBoolean(3, b);
- ps.executeUpdate();
- }
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(Jdbc.class.getName()).log(Level.SEVERE, null, ex);
- } catch (SQLException ex) {
- Logger.getLogger(Jdbc.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement