Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2566465
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-12-27 15:39:45

这篇文章主要介绍怎样通过JDBC Statement来创建一个表。
通过调用statement.execute()方法来执行sql语句,语法如下:
  1. Statement statement = conn.createStatement();
  2. // execute create SQL stetement
  3. statement.execute(sql);
完整例子如下:
  1. package org.hnrsc.jdbc;

  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;

  6. public class JDBCConnectMySQL {

  7.     private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
  8.     private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/test";
  9.     private static final String DB_USER = "root";
  10.     private static final String DB_PASSWORD = "";
  11.     
  12.     
  13.     public static void main(String[] args) {
  14.         try{
  15.             createTable();
  16.         }catch(SQLException e){
  17.             System.out.println(e.getMessage());
  18.         }
  19.     }
  20.     
  21.     private static void createTable() throws SQLException{
  22.         Connection conn = null;
  23.         Statement stmt = null;
  24.         
  25.         String sql = "CREATE TABLE t_user("
  26.              "id int auto_increment primary key,"
  27.              "username varchar(50) NOT NULL,"
  28.              "password varchar(50) NOT NULL"
  29.              ")";
  30.         
  31.         try{
  32.             conn = getDBConnection();
  33.             stmt = conn.createStatement();
  34.             System.out.println(sql);
  35.             stmt.execute(sql);
  36.             System.out.println("Table \"t_user\" is created!");
  37.         }catch(SQLException e){
  38.             System.out.println(e.getMessage());
  39.         }finally{
  40.             if(stmt!=null){
  41.                 stmt.close();
  42.             }
  43.             if(conn!=null){
  44.                 conn.close();
  45.             }
  46.         }
  47.         
  48.         
  49.     }
  50.     
  51.     private static Connection getDBConnection(){
  52.         Connection conn = null;
  53.         try{
  54.             Class.forName(DB_DRIVER);
  55.         }catch(ClassNotFoundException e){
  56.             System.out.println(e.getMessage());
  57.         }
  58.         try{
  59.             conn = DriverManager.getConnection(DB_CONNECTION,DB_USER, DB_PASSWORD);
  60.         }catch(SQLException e){
  61.             System.out.println(e.getMessage());
  62.         }
  63.         
  64.         return conn;
  65.     }

  66. }
运行结果:
CREATE TABLE t_user(id int auto_increment primary key,username varchar(50) NOT NULL,password varchar(50) NOT NULL)
Table "t_user" is created!

在数据库里可以看到创建的表结构如下:
mysql> use test
Database changed
mysql> show tables;
----------------
| Tables_in_test |
----------------
| t_user         |
----------------
1 row in set (0.08 sec)

mysql> desc t_user;
---------- ------------- ------ ----- --------- ----------------
| Field    | Type        | Null | Key | Default | Extra          |
---------- ------------- ------ ----- --------- ----------------
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| username | varchar(50) | NO   |     | NULL    |                |
| password | varchar(50) | NO   |     | NULL    |                |
---------- ------------- ------ ----- --------- ----------------
3 rows in set (0.12 sec)

阅读(945) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~