KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > db > mysqltest


1 package org.ashkelon.db;
2
3 /*
4  * mysqltest.java
5  * Created on September 19, 2001, 1:13 AM
6  */

7
8 import java.sql.Connection JavaDoc;
9 import java.sql.DriverManager JavaDoc;
10 import java.sql.PreparedStatement JavaDoc;
11 import java.sql.ResultSet JavaDoc;
12 import java.sql.SQLException JavaDoc;
13 import java.sql.Statement JavaDoc;
14
15 /**
16  * @author Eitan
17  * @version
18  */

19 public class mysqltest
20 {
21
22    public mysqltest() throws Exception JavaDoc
23    {
24       Class.forName("org.gjt.mm.mysql.Driver"); // load mysql mm driver
25
Connection JavaDoc conn = DriverManager.getConnection("jdbc:mysql://localhost/test");
26       
27       Statement JavaDoc stmt = conn.createStatement();
28       String JavaDoc sql = "select name, age from test order by age desc";
29       ResultSet JavaDoc rset = stmt.executeQuery(sql);
30
31       String JavaDoc name;
32       int age;
33       
34       while (rset.next())
35       {
36          name = rset.getString(1);
37          age = rset.getInt(2);
38          System.out.println(name + " is " + age + " years old");
39       }
40       
41       rset.close();
42       stmt.close();
43       conn.close();
44    }
45    
46    public static void oratest() throws Exception JavaDoc
47    {
48       Class.forName("oracle.jdbc.driver.OracleDriver");
49       Connection JavaDoc conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ashkelon", "system", "manager");
50       
51       String JavaDoc sql = "insert into temp (fld) values (?)";
52       PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
53       try
54       {
55          pstmt.setString(1, "thisoneistoolong");
56          pstmt.executeUpdate();
57       }
58       catch (SQLException JavaDoc ex)
59       {
60          pstmt.setString(1, "valueok");
61          pstmt.executeUpdate();
62       }
63
64       pstmt.close();
65       conn.close();
66    }
67    
68    public static void main(String JavaDoc[] args) throws Exception JavaDoc
69    {
70       //new mysqltest();
71
oratest();
72    }
73
74 }
75
Popular Tags