-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrepared_Statement.java
More file actions
42 lines (41 loc) · 1.61 KB
/
Prepared_Statement.java
File metadata and controls
42 lines (41 loc) · 1.61 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
import java.sql.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test{
public static void main(String[] args) throws ClassNotFoundException, SQLException, InvalidDataException {
String phone = "9876543210 09876543210 919876543210";
String NAME = "mlknkv455 Ram j767 Aman";
Pattern p = Pattern.compile("(0|91)+[7-9][0-9]{9}");
Pattern p1 = Pattern.compile("(R|A)+\\w");
Matcher m = p.matcher(phone);
Matcher m1 = p1.matcher(NAME);
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/internal_marks","root","");
PreparedStatement s = con.prepareStatement("insert into iinfoo values(?,?)");
while(m1.find()&&m.find()) {
s.setString(1, m.group());
s.setString(2, m1.group());
s.executeUpdate();
}
Statement ss = con.createStatement();
ResultSet rs = ss.executeQuery("select * from iinfoo");
int i=1;
while(rs.next()) {
if(i==1||i==10||i==11) {
String k = rs.getString(2);
if (k != "Snigdha" && k != "Ram" && k != "Aman") {
throw new InvalidDataException("String not found.");
} else {
System.out.println(rs.getString(1) + " " + rs.getString(2));
}
}
i++;
}
con.close();
}
}
class InvalidDataException extends Exception{
public InvalidDataException(String a){
super(a);
}
}