-
Notifications
You must be signed in to change notification settings - Fork 0
/
C1.java
35 lines (31 loc) · 1.07 KB
/
C1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Scanner;
import java.util.StringTokenizer;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class C1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "root");
Scanner scanner=new Scanner(System.in);
System.out.println("Enter URL: ");
String longUrl=scanner.nextLine();
String rand=String.valueOf(Math.random()*100000);
StringTokenizer stringTokenizer=new StringTokenizer(rand, ".");
rand=stringTokenizer.nextToken();
String baseUrl="http://www.goo.gl.com/";
Statement statement=(Statement)connection.createStatement();
String shortenedUrl=baseUrl + rand;
statement.execute("INSERT INTO url_shortner_table_1 VALUES ('" + longUrl + "', '" + shortenedUrl + "')");
System.out.println(shortenedUrl);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}