Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* http://www.sqlitetutorial.net/sqlite-java/create-database/
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package aa;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.Scanner;
- public class Insertadatos {
- public static void main(String[] args) throws SQLException {
- String url = "jdbc:mysql://localhost:3306/agenda";
- String user = "root";
- String password = "";
- Connection myConn = null;
- PreparedStatement myStmt = null;
- Scanner scanner = null;
- try {
- // 0. read user input from command line: codigo nombre apellido tlf
- scanner = new Scanner(System.in);
- /*System.out.print("Enter your last codigo: ");
- int codigo = scanner.nextInt();*/
- System.out.print("Enter your last nombre: ");
- String nombre = scanner.next();
- System.out.print("Enter your first apellido: ");
- String apellido = scanner.next();
- System.out.print("Enter your tlf: ");
- String tlf = scanner.next();
- // 1. Get a connection to database
- myConn = DriverManager.getConnection(url, user, password);
- // 2. Create a statement
- String sql = "insert into agenda "
- + " (codigo, nombre, apellido, tlf)" + " values (null, ?, ?, ?)";
- myStmt = myConn.prepareStatement(sql);
- // set param values
- //myStmt.setInt(1, codigo);
- myStmt.setString(1, nombre);
- myStmt.setString(2, apellido);
- myStmt.setString(3, tlf);
- // 3. Execute SQL query
- myStmt.executeUpdate();
- System.out.println("Insert complete.");
- } catch (Exception exc) {
- exc.printStackTrace();
- } finally {
- if (myStmt != null) {
- myStmt.close();
- }
- if (myConn != null) {
- myConn.close();
- }
- if (scanner != null) {
- scanner.close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement