EvilZone
Programming and Scripting => Beginner's Corner => : blackeagle 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 ?
-
Can you post your code? it helps in understanding your question.
-
SELECT number FROM numbers WHERE id=userinput
well thats what i need to do to a database using java
-
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
-
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.
-
my problem is that i'm new to all this didn't do much just managed to connect to the database
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();
}
}
}
-
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.
-
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.
-
@Deque that wasn't my problem anw i managed to make it work
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();
}
}
}
-
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.
-
Instead of using Statement Try using PreparedStatement as it is more safe .
Also when you want call a Stored procedure use CallableStatement .