日期:2014-05-16 浏览次数:20518 次
????? 数据库技术
实例293 加载JDBC驱动程序
import java.sql.DriverManager; import java.util.Enumeration; public class UseMySQLDriver { public static void main(String[] args) { try { // 初始化并加载MySQL驱动程序 Class.forName("com.mysql.jdbc.Driver"); Enumeration em = DriverManager.getDrivers(); // 显示驱动程序信息 while (em.hasMoreElements()) { System.out.println(em.nextElement()); } // 处理加载数据库中可能出现的异常 } catch (Exception e) { System.out.println("加载数据库驱动程序出现异常"); } } }
?
实例294 通过JDBC对数据库进行查询
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; public class JDBCSearch { static ResultSet rs = null; public static void main(String[] args) throws SQLException { try { Class.forName("com.mysql.jdbc.Driver");// 加载驱动程序 Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/myuser", "root", "root");// 建立链接 Statement usest = con.createStatement();// 创建Statement对象 usest.setMaxRows(100);// 设置选项 ResultSet users = usest.executeQuery("select * from gods");// 执行查询 System.out.println("ResultSet MaxRows:" + usest.getMaxRows()); System.out.println("Query Time Out:" + usest.getMaxRows() + "\n"); String sqlstr = "select * from gods where price>?";// 执行参数查询 PreparedStatement ps = con.prepareStatement(sqlstr);// 创建PreparedStatement对象 ps.setString(1, "15"); rs = ps.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData();// 获取结果集中的列名及其类型 int cc = rsmd.getColumnCount(); System.out.println("ColumnName\t\tColumnType"); for (int i = 1; i <= cc; i++) { System.out.println(rsmd.getColumnClassName(i) + "\t" + rsmd.getColumnTypeName(i)); } con.close();// 关闭对象 } catch (ClassNotFoundException e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
?
实例295 数据库更新
import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCUpdate { public static void main(String[] args) throws SQLException, UnsupportedEncodingException { String name, type, size, strn; String depict, price, offers; try { Class.forName("com.mysql.jdbc.Driver");// 加载驱动程序 Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/myuser", "root", "root");// 建立链接 Statement st = con.createStatement();// 创建Statement对象 strn = "alter table gods add column size varchar(12)";// 向表中添加列 st.executeUpdate(strn); strn = "INSERT INTO gods(name, type, depict,price, offers,sizes) VALUES ('Shiseido', 'Cosmetics','meibaibs','280','SOFINA','250ml');"; st.executeUpdate(strn); strn = "INSERT INTO gods(name, type, depict,price, offers) VALUES ('Lamp','luminaire','Cartoon images','28','no');"; st.executeUpdate(strn); ResultSet rs = st.executeQuery("select * from gods");// 执行查询 System.out.println("商品名称\t商品类别\t商品描述\t商品价格\t优惠商品尺码"); while (rs.next()) { name = new String(rs.getString("name").getBytes("iso8859-1"), "gb2312"); price = new String(rs.getString("price").getBytes("iso8859-1"), "gb2312"); depict = new String(rs.getString("depict") .getBytes("iso8859-1"), "gb2312"); type = new String(rs.getString("type").getBytes("iso8859-1"), "gb2312"); size = rs.getString("size"); offers = new String(rs.getString("offers") .getBytes("iso8859-1"), "gb2312"); System.out.println(name + " " + type + " " + depict + " " + price + " " + offers + " " + size); } con.close();// 关闭连接 } catch (ClassNotFoundException e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
?