· 7 years ago · Feb 13, 2019, 05:50 AM
1import java.sql.*;
2
3public class Jdbc10 {
4 public static void main(String args[]){
5 System.out.println(
6 "Copyright 2004, R.G.Baldwin");
7 try {
8 Statement stmt;
9 ResultSet rs;
10
11 //Register the JDBC driver for MySQL.
12 Class.forName("com.mysql.jdbc.Driver");
13
14 //Define URL of database server for
15 // database named JunkDB on the localhost
16 // with the default port number 3306.
17 String url =
18 "jdbc:mysql://localhost:3306/JunkDB";
19
20 //Get a connection to the database for a
21 // user named auser with the password
22 // drowssap, which is password spelled
23 // backwards.
24 Connection con =
25 DriverManager.getConnection(
26 url,"auser", "drowssap");
27
28 //Display URL and connection information
29 System.out.println("URL: " + url);
30 System.out.println("Connection: " + con);
31
32 //Get a Statement object
33 stmt = con.createStatement();
34
35 //As a precaution, delete myTable if it
36 // already exists as residue from a
37 // previous run. Otherwise, if the table
38 // already exists and an attempt is made
39 // to create it, an exception will be
40 // thrown.
41 try{
42 stmt.executeUpdate("DROP TABLE myTable");
43 }catch(Exception e){
44 System.out.print(e);
45 System.out.println(
46 "No existing table to delete");
47 }//end catch
48
49 //Create a table in the database named
50 // myTable.
51 stmt.executeUpdate(
52 "CREATE TABLE myTable(test_id int," +
53 "test_val char(15) not null)");
54
55 //Insert some values into the table
56 stmt.executeUpdate(
57 "INSERT INTO myTable(test_id, " +
58 "test_val) VALUES(1,'One')");
59 stmt.executeUpdate(
60 "INSERT INTO myTable(test_id, " +
61 "test_val) VALUES(2,'Two')");
62 stmt.executeUpdate(
63 "INSERT INTO myTable(test_id, " +
64 "test_val) VALUES(3,'Three')");
65 stmt.executeUpdate(
66 "INSERT INTO myTable(test_id, " +
67 "test_val) VALUES(4,'Four')");
68 stmt.executeUpdate(
69 "INSERT INTO myTable(test_id, " +
70 "test_val) VALUES(5,'Five')");
71
72 //Get another statement object initialized
73 // as shown.
74 stmt = con.createStatement(
75 ResultSet.TYPE_SCROLL_INSENSITIVE,
76 ResultSet.CONCUR_READ_ONLY);
77
78 //Query the database, storing the result
79 // in an object of type ResultSet
80 rs = stmt.executeQuery("SELECT * " +
81 "from myTable ORDER BY test_id");
82
83 //Use the methods of class ResultSet in a
84 // loop to display all of the data in the
85 // database.
86 System.out.println("Display all results:");
87 while(rs.next()){
88 int theInt= rs.getInt("test_id");
89 String str = rs.getString("test_val");
90 System.out.println("\ttest_id= " + theInt
91 + "\tstr = " + str);
92 }//end while loop
93
94 //Display the data in a specific row using
95 // the rs.absolute method.
96 System.out.println(
97 "Display row number 2:");
98 if( rs.absolute(2) ){
99 int theInt= rs.getInt("test_id");
100 String str = rs.getString("test_val");
101 System.out.println("\ttest_id= " + theInt
102 + "\tstr = " + str);
103 }//end if
104
105 //Delete the table and close the connection
106 // to the database
107 stmt.executeUpdate("DROP TABLE myTable");
108 con.close();
109 }catch( Exception e ) {
110 e.printStackTrace();
111 }//end catch
112 }//end main
113}//end class Jdbc10
114
115
116
117Listing 42 Jdbc10.java
118
119Copyright 2004, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.
120About the author
121
122Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java, C#, and XML. In addition to the many platform and/or language independent benefits of Java and C# applications, he believes that a combination of Java, C#, and XML will become the primary driving force in the delivery of structured information on the Web.
123
124Richard has participate