· 9 years ago · Sep 19, 2017, 07:42 AM
1import java.sql.*;
2
3 public class Connect
4 {
5 public static void main (String[] args)
6 {
7 Connection conn = null;
8
9 try
10 {
11 String userName = "root";
12 String password = "aaaaaaa";
13 String url = "jdbc:mysql://localhost:3306/user_geometry";
14 System.out.println("wat");
15 try{
16 Class.forName ("com.mysql.jdbc.Driver").newInstance ();
17 }
18 catch(Exception e)
19 {
20 System.out.println("Driver not found");
21 }
22 System.out.println("wat2");
23 conn = DriverManager.getConnection (url, userName, password);
24 System.out.println ("Database connection established");
25 Statement s = conn.createStatement ();
26
27 //-------------------------------
28// int count;
29//
30// System.out.println("Adding rows of animals");
31// Statement s = conn.createStatement ();
32 s.executeUpdate ("DROP TABLE IF EXISTS animal");
33// s.executeUpdate (
34// "CREATE TABLE animal ("
35// + "id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
36// + "PRIMARY KEY (id),"
37// + "name CHAR(40), category CHAR(40))");
38// count = s.executeUpdate (
39// "INSERT INTO animal (name, category)"
40// + " VALUES"
41// + "('snake', 'reptile'),"
42// + "('frog', 'amphibian'),"
43// + "('tuna', 'fish'),"
44// + "('racoon', 'mammal')");
45// s.close ();
46 //---------------------------------
47 System.out.println("Creating table copy");
48// s = conn.createStatement ();
49// s.executeQuery ("SELECT * FROM user_geometry");
50 s.executeUpdate("CREATE TABLE bmt LIKE BST");
51// ResultSet rs = s.getResultSet ();
52// count = 0;
53// while (rs.next ())
54// {
55// System.out.println(rs.next());
56// }
57// rs.close ();
58// s.close ();
59// System.out.println (count + " rows were retrieved");
60 }
61 catch (Exception e)
62 {
63 System.err.println ("Cannot connect to database server");
64 }
65 finally
66 {
67 if (conn != null)
68 {
69 try
70 {
71 conn.close ();
72 System.out.println ("Database connection terminated");
73 }
74 catch (Exception e) { /* ignore close errors */ }
75 }
76 }
77 }
78 }