Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 database;
- import java.sql.*;
- import java.util.Scanner;
- /**
- *
- * @author 17951A05F3
- */
- public class Database {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- while(true)
- {
- System.out.println("1.retrieve\n2.update\n3.exit\n");
- Scanner sc = new Scanner(System.in);
- int temp = sc.nextInt();
- if(temp==1){
- Database.retrieve();
- }
- if(temp == 2){
- Database.update();
- }
- else{
- break;
- }
- }
- }
- public static void retrieve(){
- try(Connection conn = DriverManager.getConnection("Jdbc:mysql//localhost:3306/mysql","root","1234")){
- Statement st1 = conn.createStatement();
- ResultSet res = st1.executeQuery("select * form student");
- while(res.next()){
- System.out.println("id"+res.getInt("id")+"name"+res.getString("name"));
- }
- }catch(SQLException ex){
- ex.printStackTrace();
- }
- }
- public static void update(){
- try(Connection conn = DriverManager.getConnection("Jdbc:mysql//localhost:3306/mysql","root","1234")){
- Statement s = conn.createStatement();
- s.executeUpdate("Update student SET id = 3 where name = 'hello3'");
- }catch(SQLException ex){
- ex.printStackTrace();
- }
- }
- // TODO code application logic here
- }
Add Comment
Please, Sign In to add comment