· 9 years ago · Jun 05, 2017, 09:58 PM
1package Reviseknowledge;
2import java.sql.Connection;
3import java.sql.DriverManager;
4import java.sql.PreparedStatement;
5import java.sql.ResultSet;
6import java.util.ArrayList;
7
8public class Learn {
9
10 public static void main(String[] args) throws Exception {
11
12
13
14 }
15
16
17 static String url = "jdbc:mysql://localhost:3306/guest";
18 static String username = "root";
19 static String password = "guest123";
20
21
22
23
24 public static void customSelection () throws Exception {
25
26 try {
27
28 Connection conn = DriverManager.getConnection(url,username,password);
29
30 PreparedStatement custom = conn.prepareStatement ( "SELECT * from stebuklas where numbers < 10 ");
31
32 ResultSet res = custom.executeQuery ();
33
34
35
36 while ( res.next() ) {
37
38 System.out.println ( res.getString ("first") + "\t" + res.getString ("last") + res.getInt("numbers") );
39
40 }
41
42
43
44
45 }
46 catch(Exception e){ System.out.println(e); }
47 }
48
49
50public static void update () throws Exception { // UPDATE UPDATE, remove old ones and replace them.
51
52 try{
53
54 Connection conn = DriverManager.getConnection(url,username,password);
55
56 PreparedStatement update = conn.prepareStatement ( "UPDATE stebuklas set last='boboz' where numbers =7");
57
58 update.executeUpdate();
59 }
60 catch(Exception e){ System.out.println(e); }
61
62 System.out.println ("enri was replaced (updated)"); ////
63
64 }
65
66
67 public static void createTable () throws Exception { // CREATE CREATE NEW TABLET
68
69 try {
70
71 Connection conn = DriverManager.getConnection(url,username,password);
72
73 PreparedStatement create = conn.prepareStatement("CREATE TABLE IF NOT EXISTS stebuklas "
74 + "( first varchar(253), "
75 + "last varchar(250),"
76 + "numbers integer(50) )");
77 create.executeUpdate();
78
79 }
80
81 catch(Exception e){ System.out.println(e); }
82
83 finally { System.out.println ("table was madee"); } ////
84
85 }
86
87
88 public static void post () throws Exception { // INSERT INSERT VALUES
89
90 String var1 = "enri";
91 String var2 = "lerp";
92 double var3 = 7;
93
94 try {
95 Connection conn = DriverManager.getConnection(url,username,password);
96 PreparedStatement posted = conn.prepareStatement (" INSERT INTO stebuklas "
97 + "(first,last,numbers) VALUES ('"+var1+"', '"+var2+"', '"+var3+"')");
98
99 posted.executeUpdate ();
100 posted.executeUpdate ();
101
102 }
103 catch(Exception e){ System.out.println(e); }
104 finally {System.out.println ("values inserted to the table.");} //////
105
106
107 }
108
109
110 public static void get() throws Exception { // SELECT SELECT SELECT
111 try{
112 Connection conn = DriverManager.getConnection(url,username,password);
113 PreparedStatement statement = conn.prepareStatement("SELECT * FROM stebuklas LIMIT 2");
114 PreparedStatement stat = conn.prepareStatement("SELECT * FROM stebuklas LIMIT 3");
115
116 ResultSet result = statement.executeQuery ();
117 ResultSet res = stat.executeQuery ();
118
119 while (result.next() ) {
120
121 System.out.println ( result.getString("first") );
122
123 }
124
125
126 while ( res.next() ) {
127
128 System.out.println ( res.getInt("numbers") );
129
130 }
131
132 }
133 catch(Exception e){ System.out.println(e); }
134
135 }
136
137
138
139}