EvilZone

Programming and Scripting => Beginner's Corner => Topic started by: blackeagle on September 01, 2015, 12:16:00 PM

Title: java search database
Post by: blackeagle on September 01, 2015, 12:16:00 PM
i'll start by saying i only know some basic java i'm new to all this.
ok so far i managed to make a database using sql and i managed to connect to it using java .
the database is all about names and phone numbers now i want some1 to guide me .
what i want to do is ask the user to enter a name in order to get its phone number any ideas what should i do ?
Title: Re: java search database
Post by: Lenoch on September 01, 2015, 02:33:46 PM
Can you post your code? it helps in understanding your question.
Title: Re: java search database
Post by: blackeagle on September 01, 2015, 06:19:03 PM
SELECT number FROM numbers WHERE id=userinput

well thats what i need to do to a database using java
Title: Re: java search database
Post by: Lenoch on September 01, 2015, 06:26:53 PM
Hmm so no java code. Have you tried looking up on google?


I found this: https://www.youtube.com/watch?v=2i4t-SL1VsU
and this: https://docs.oracle.com/javase/tutorial/jdbc/index.html



Title: Re: java search database
Post by: iTpHo3NiX on September 01, 2015, 08:03:46 PM
Blackeagle, as Lenoch requested, your table structure and Java code are needed to better help you with your issue, as it currently stands, it's like you're going to a vehicle parts store saying you need a timing belt, however refusing to give them the make, model and year of your vehicle.
Title: Re: java search database
Post by: blackeagle on September 01, 2015, 09:20:24 PM
my problem is that i'm new to all this didn't do much just managed to connect to the database
Code: [Select]
import java.util.Scanner;
import java.sql.*;
public class testDatabase {

public static void main (String args[]) {
      Scanner in=new Scanner(System.in);
     
      try {
      Connection c= null;
      Class.forName("org.sqlite.JDBC");
   
      c = DriverManager.getConnection("jdbc:sqlite:numbers.db");
         System.out.println("connected");
}
  catch (Exception e)
     { 
         e.printStackTrace(); 
     }

    }
 }
Title: Re: java search database
Post by: Lenoch on September 02, 2015, 12:07:42 AM
Just check the link I posted. It explains how to execute a statement. It's just nothing more then defining an object and executing a method.
Title: Re: java search database
Post by: Deque on September 02, 2015, 09:06:00 AM
what i want to do is ask the user to enter a name in order to get its phone number any ideas what should i do ?

How you get user input is one of the first things you learn in every Java tutorial. Please learn the basics of Java first.
If that was not your problem, then start with including what you can already achieve. That means ask the user for input, save that input in a variable. Only then you may start thinking about how you can put that into the database.
Title: Re: java search database
Post by: blackeagle on September 02, 2015, 03:05:30 PM
@Deque that wasn't my problem anw i managed to make it work
Code: [Select]
import java.util.Scanner;
import java.sql.*;
public class testDatabase{

 
public static void main (String args[]) {
           Scanner in=new Scanner(System.in);
     
      try {
  String a=in.next();
      Connection c= null;
      Class.forName("org.sqlite.JDBC");
      c = DriverManager.getConnection("jdbc:sqlite:numbers.db");
         System.out.println("connected");
Statement myStmt =c.createStatement();
ResultSet myRs =myStmt.executeQuery("SELECT * FROM numbers WHERE NAME='"+a+"'");
while(myRs.next()){
System.out.println(myRs.getString("name")+"\t\t\t"+myRs.getInt("number"));
}
}

  catch (Exception e)
     { 
         e.printStackTrace(); 
     }

    }
 }
   
Title: Re: java search database
Post by: gray-fox on September 02, 2015, 08:22:17 PM
I'm not very fluent in Java, but seems like you should check this:
https://www.owasp.org/index.php/Preventing_SQL_Injection_in_Java

It's good practise to do safe coding in general.
Title: Re: java search database
Post by: Abyss on October 10, 2015, 08:32:55 AM
Instead of using Statement Try using PreparedStatement as it is more safe .
Also when you want call a Stored procedure use CallableStatement .