I need some guidance on my current obstacle with this application. If you can po
ID: 3764879 • Letter: I
Question
I need some guidance on my current obstacle with this application. If you can point me in the right direction will help. Basically I have twitter like application with a sql database storing the "tweets" and the users data.The user's nickname always start with a "@". Now everything is working but i need to add the functionality that if I send a twit and "mentioned" a user, meaning I add @nickname , then that person I mentioned should see it on his messages. I think I can figure it out, but i'm just confused on how to get started, specifically when I'm getting the "tweet" how can I find a "@" and return the word that contains the "@" ? The next step should probably be to see if that nickname exists and find the userID by the nickname and update their messages? Please help me out.
Explanation / Answer
To complete your program have to follow the following steps:
1.Import the packages: Requires that you include the packages containing the JDBC classes needed for database programming. Most often, using import java.sql.* will suffice.
2.Register the JDBC driver: Requires that you initialize a driver so you can open a communications channel with the database.
3.Open a connection: Requires using the DriverManager.getConnection() method to create a Connection object, which represents a physical connection with a database server.
4.Execute a query: Requires using an object of type Statement for building and submitting an SQL statement to sort records from a table. These Queries make use of asc and desc clauses to sort data in ascending and descening orders.
5.Clean up the environment: Requires explicitly closing all database resources versus relying on the JVM's garbage collection.
this is sample program to sort the names and their massages:
import java.sql.*;
public class JDBCExample
{
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
System.out.println("Creating statement...");
stmt = conn.createStatement();
// Extract records in ascending order by first name.
System.out.println("Fetching records in ascending order...");
String sql = "SELECT id, first, last, age FROM Registration" +
" ORDER BY first ASC";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
// Extract records in descending order by name.
System.out.println("Fetching records in descending order...");
sql = "SELECT id, first, last, age FROM Registration" +
" ORDER BY first DESC";
rs = stmt.executeQuery(sql);
while(rs.next()){
//colun names are retrived
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
//Display data
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
rs.close();
}catch(SQLException se){
//error handling
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}