· 9 years ago · Jun 04, 2017, 09:32 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 createTable ();
13
14
15 }
16
17
18 public static void createTable () throws Exception {
19
20 try {
21
22 Connection con = getConnection ();
23 java.sql.PreparedStatement create = con.prepareStatement("CREATE TABLE IF NOT EXISTS stebuklas "
24 + "( first varchar(253), "
25 + "last varchar(250),"
26 + "numbers integer(50) )");
27 create.executeUpdate();
28
29 }
30
31 catch(Exception e){ System.out.println(e); }
32
33 finally { System.out.println ("table was madee"); }
34
35 }
36
37
38
39
40
41 /*
42
43
44 public static void post () throws Exception {
45
46 String var1 = "ann";
47 String var2 = "lesh";
48 double var3 = 5;
49
50 try {
51 Connection conn = getConnection ();
52 PreparedStatement posted = conn.prepareStatement (" INSERT INTO stebuklas "
53 + "(first,last,numbers) VALUES ('"+var1+"', '"+var2+"', '"+var3+"')");
54
55 posted.executeUpdate ();
56 posted.executeUpdate ();
57
58 }
59 catch(Exception e){ System.out.println(e); }
60
61 finally {System.out.println ("values inserted to the table.");}
62
63
64 }
65
66
67 */
68
69 /*
70 public static void get() throws Exception {
71 try{
72 Connection conn = getConnection ();
73 PreparedStatement statement = conn.prepareStatement("SELECT * FROM stebuklas LIMIT 2");
74 PreparedStatement stat = conn.prepareStatement("SELECT * FROM stebuklas LIMIT 3");
75
76 ResultSet result = statement.executeQuery ();
77 ResultSet res = stat.executeQuery ();
78
79 while (result.next() ) {
80
81 System.out.println ( result.getString("first") );
82
83 }
84
85
86 while ( res.next() ) {
87
88 System.out.println ( res.getInt("numbers") );
89
90 }
91
92
93 }
94 catch(Exception e){ System.out.println(e); }
95
96
97
98 }
99 */
100
101 public static Connection getConnection() throws Exception{
102
103 try{
104 String driver = "com.mysql.jdbc.Driver";
105 String url = "jdbc:mysql://localhost:3306/guest";
106 String username = "root";
107 String password = "guest123";
108 Class.forName(driver);
109 Connection conn = DriverManager.getConnection(url,username,password);
110 System.out.println("Connected");
111 return conn;
112 }
113 catch(Exception e){
114 System.out.println(e);
115 }
116 return null;
117 }
118
119
120}