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 mysqlconnect;
- /**
- *
- * @author Jami
- */
- import java.sql.*;
- import java.util.Scanner;
- public class DBConnect {
- private Connection con;
- private Statement st;
- private ResultSet rs;
- public DBConnect(){
- try{
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","");
- /*localhost = hostname, 3306 = portnumber, user = databasename, root = databaseusername...
- no need of password write now...*/
- st = con.createStatement();
- }
- catch(Exception ex){
- System.out.println("Error: "+ex);
- }
- }
- public void insertData(){
- String name="",pas="";
- Scanner sc = new Scanner(System.in);
- System.out.print("User name: ");
- name = sc.next();
- System.out.print("Password: ");
- pas = sc.next();
- try{
- PreparedStatement inserted = con.prepareStatement("INSERT INTO INFO(name,password)VALUES('"+name+"','"+pas+"')");
- inserted.executeUpdate();
- }
- catch(Exception ex){
- System.out.println(ex);
- }
- }
- public void getdata(){
- try{
- String q = "select * from info";
- rs = st.executeQuery(q);
- System.out.println("Record");
- while(rs.next()){
- String n = rs.getString("name");
- String p = rs.getString("password");
- int i = rs.getInt("id");
- System.out.println("ID: "+i+" "+"Name: "+n+ " "+"Password: "+p);
- }
- }
- catch(Exception ex){
- System.out.println(ex);
- }
- }
- public void deleteData(){
- Scanner sc = new Scanner(System.in);
- int i = sc.nextInt();
- try{
- PreparedStatement deleted = con.prepareStatement("DELETE FROM INFO WHERE id = '"+i+"'");
- deleted.executeUpdate();
- }
- catch(Exception ex){
- System.out.println(ex);
- }
- }
- }
Add Comment
Please, Sign In to add comment