-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjdbc_input_output_into_and_from_database.java
More file actions
84 lines (76 loc) · 2.52 KB
/
jdbc_input_output_into_and_from_database.java
File metadata and controls
84 lines (76 loc) · 2.52 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package JDBC;
import java.util.*;
import java.lang.*;
import java.sql.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// for storing the valid emails from a long string valid mails
class insert_dataToDatabase {
public void insert(String s) throws ClassNotFoundException,SQLException{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:8809/RahulDatabase","root","root");
PreparedStatement st = con.prepareStatement("insert into Data values (?)");
Pattern p = Pattern.compile("[a-zA-Z][a-zA-Z0-9._]+@(gmail|hotmail|yahoo|rediff|outlook).com");
Matcher mat =p.matcher(s);
boolean T=true;
if (mat.find()==false)
{
try {
throw new exc();
}catch (exc e){}
}
else
{
while (T) {
st.setString(1, mat.group());
st.execute();
T = mat.find();
}
}
con.close();
}
}
//Data fetching from the stored database only the last string valid emails
class showww {
public void data() throws ClassNotFoundException,SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc:mysql://localhost:8809/RahulDatabase","root","root");
ResultSet st = con.prepareStatement("select * from data").executeQuery();
boolean T = true ;
if(st.next()==false)
{
try{
throw new exc(8);
}catch (exc e){return;}
}
else
{
while (T){
System.out.println(st.getString("email"));
T=st.next();
}
}
con.close();
}
}
// main method
public class JDBC_practice {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the data....");
insert_dataToDatabase obj= new insert_dataToDatabase();
obj.insert(sc.nextLine());
showww sho = new showww();
sho.data();
System.out.println("rest of the java programs....🤓");
}
}
// this is for the custom exception
class exc extends RuntimeException{
exc(){
System.out.println("No match found!!!");
}
exc(int e){
System.out.println("No Updated data will found");
}
}