1 package org.ashkelon.db; 2 3 7 8 import java.sql.Connection ; 9 import java.sql.DriverManager ; 10 import java.sql.PreparedStatement ; 11 import java.sql.ResultSet ; 12 import java.sql.SQLException ; 13 import java.sql.Statement ; 14 15 19 public class mysqltest 20 { 21 22 public mysqltest() throws Exception 23 { 24 Class.forName("org.gjt.mm.mysql.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test"); 26 27 Statement stmt = conn.createStatement(); 28 String sql = "select name, age from test order by age desc"; 29 ResultSet rset = stmt.executeQuery(sql); 30 31 String 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 47 { 48 Class.forName("oracle.jdbc.driver.OracleDriver"); 49 Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ashkelon", "system", "manager"); 50 51 String sql = "insert into temp (fld) values (?)"; 52 PreparedStatement pstmt = conn.prepareStatement(sql); 53 try 54 { 55 pstmt.setString(1, "thisoneistoolong"); 56 pstmt.executeUpdate(); 57 } 58 catch (SQLException 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 [] args) throws Exception 69 { 70 oratest(); 72 } 73 74 } 75 | Popular Tags |