Author Topic: java search database  (Read 1362 times)

0 Members and 1 Guest are viewing this topic.

Offline blackeagle

  • Serf
  • *
  • Posts: 49
  • Cookies: -2
    • View Profile
java search database
« 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 ?

Offline Lenoch

  • EZ's Menstruator
  • VIP
  • Serf
  • *
  • Posts: 49
  • Cookies: 37
    • View Profile
Re: java search database
« Reply #1 on: September 01, 2015, 02:33:46 pm »
Can you post your code? it helps in understanding your question.


Quote
<m0dem> I find evilzone is a really HQ community

Offline blackeagle

  • Serf
  • *
  • Posts: 49
  • Cookies: -2
    • View Profile
Re: java search database
« Reply #2 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

Offline Lenoch

  • EZ's Menstruator
  • VIP
  • Serf
  • *
  • Posts: 49
  • Cookies: 37
    • View Profile
Re: java search database
« Reply #3 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





Quote
<m0dem> I find evilzone is a really HQ community

Offline iTpHo3NiX

  • EZ's Pirate Captain
  • Administrator
  • Titan
  • *
  • Posts: 2920
  • Cookies: 328
    • View Profile
    • EvilZone
Re: java search database
« Reply #4 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.
[09:27] (+lenoch) iTpHo3NiX can even manipulate me to suck dick
[09:27] (+lenoch) oh no that's voluntary
[09:27] (+lenoch) sorry

Offline blackeagle

  • Serf
  • *
  • Posts: 49
  • Cookies: -2
    • View Profile
Re: java search database
« Reply #5 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(); 
     }

    }
 }

Offline Lenoch

  • EZ's Menstruator
  • VIP
  • Serf
  • *
  • Posts: 49
  • Cookies: 37
    • View Profile
Re: java search database
« Reply #6 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.


Quote
<m0dem> I find evilzone is a really HQ community

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: java search database
« Reply #7 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.

Offline blackeagle

  • Serf
  • *
  • Posts: 49
  • Cookies: -2
    • View Profile
Re: java search database
« Reply #8 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(); 
     }

    }
 }
   

Offline gray-fox

  • Knight
  • **
  • Posts: 208
  • Cookies: 52
    • View Profile
Re: java search database
« Reply #9 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.

Offline Abyss

  • Serf
  • *
  • Posts: 21
  • Cookies: -5
    • View Profile
Re: java search database
« Reply #10 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 .
« Last Edit: October 10, 2015, 08:33:29 am by Abyss »