/Users/lyon/j4p/src/net/server/servlets/Execute.java

1    package net.server.servlets; 
2     
3    import java.sql.Connection; 
4    import java.sql.DriverManager; 
5    import java.sql.Statement; 
6     
7    public class Execute { 
8        public static String getSql( 
9                String firstname, 
10               String middlename, 
11               String lastname, 
12               String address1, 
13               String address2, 
14               String phone1, 
15               String phone2, 
16               String phone3) { 
17    
18           return "insert into phonelist" 
19                   + "(firstname,middlename, lastname," 
20                   + "address1,address2,phone1,phone2,phone3) values " 
21                   + "(" + quote(firstname) 
22                   + quote(middlename) 
23                   + quote(lastname) 
24                   + quote(address1) 
25                   + quote(address2) 
26                   + quote(phone1) 
27                   + quote(phone2) 
28                   + nocommaQuote(phone3) + ")"; 
29       } 
30    
31       public static String nocommaQuote(String s) { 
32           return "'" + s + "'"; 
33       } 
34    
35       public static String quote(String s) { 
36           return "'" + s + "',"; 
37       } 
38    
39       public static void main(String args[]) { 
40           // firstname,lastname," 
41           //    + "address1,address2,phone1,phone2,phone3 
42           String sql = getSql("doug", "adams", "lyon", "1313", "mockingbird lane", 
43                   "203-877-0899", "9990999", "1234567"); 
44           System.out.println(sql); 
45           Execute.statement(sql); 
46       } 
47    
48       public static void test1(String args[]) { 
49           Execute.statement( 
50                   "insert into phonelist(firstname,lastname) values ('doug','lyon')" 
51           ); 
52       } 
53    
54       public static void statement(String sql) { 
55           try { 
56               String url = net.server.db.SqlBean.getUrl(); 
57               String driver = net.server.db.SqlBean.getDriver(); 
58               System.out.println("trying sql statement:" + sql); 
59               Class.forName(driver).newInstance(); 
60               Connection c = DriverManager.getConnection( 
61                       url, net.server.db.SqlBean.getUserId(), net.server.db.SqlBean.getPassword() 
62               ); 
63               Statement s = c.createStatement(); 
64               s.execute(sql); 
65               s.close(); 
66               c.close(); 
67               System.out.println("it worked! Now I am the destroyer of worlds!"); 
68           } catch (Exception e) { 
69               e.printStackTrace(); 
70           } 
71       } 
72   } 
73