使用DDL,DML语言对数据库进行基本操作。

来源:互联网 发布:天刀贴吧女神捏脸数据 编辑:程序博客网 时间:2024/05/01 17:26

n 创建表并插入数据及修改数据:

import java.sql.Connection;import java.sql.Statement;public class CreateTable {public static void main(String[] args) {Connection con = null;try {// 通过连接池来获得一个连接con = DBCon.getConnectionFromPooledDataSource("jdbcPool/mydatasource");// 创建语句对象Statement st = con.createStatement();// 创建表的SQL语句String sql = "create table student(id int,name char(30),age int)";// 执行完SQL语句的结果boolean b = st.execute(sql);if (b) {System.out.println("create success");} else {System.out.println("create fail");}// 插入数据到student表sql = "insert into student values(1,'andy',47)"+ "insert into student values(2,'jacky',53)"+ "insert into student values(3,'周润发',51)"+ "insert into student values(4,'谢贤',60)";// 执行完SQL语句的结果b = st.execute(sql);if (b) {System.out.println("insert success");} else {System.out.println("create fail");}// 更新表数据sql = "update  student set name='刘德华' where id=1";int rows = st.executeUpdate(sql);// 如果更新成功,rows肯定是大于1的值if (rows > 0)System.out.println("update success");elseSystem.out.println("update fail");} catch (Exception e) {e.printStackTrace();} finally {try {if (con != null)con.close();} catch (Exception e) {e.printStackTrace();}}}}


原创粉丝点击