Write a Java Program that loads names and phone numbers from a text file where the data is organized as one line per record and each filed in a record are separated by a tab (\t). It takes a name or phone number as input and prints the corresponding other value from the hash table with the database instead of a text file.

Program Code:
import java.sql.*;
public class JdbcExample
{
static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
static final String DB_URL="jdbc:mysql://localhost/jdbc";
static final String USER="root";
static final String PASS="Gcet@05";
public static void main(String args[])
{
Connection conn=null;
Statement stmt=null;
try
{
System.out.println("connecting to database--");
conn=DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("creating statement--");
stmt=conn.createStatement();
String sql;
System.out.println(“Enter the name as in database”);
String nm=sc.next();
sql="SELECT phone FROM phonenub where name="+nm;
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
int phone=rs.getInt("phone");
System.out.println("phone:"+phone);
}
rs.close();
stmt.close();
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
}

Output :
connecting to database—
creating statement—
Enter the name as in database
“surya”
Phone: 123

Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: