10bf43f7dc75001d117f97694e587497

来自「java程序设计教程的源码」· 代码 · 共 36 行

TXT
36
字号
/*【例12-2】  创建学生表tblstudent(sno,sname,sex,score),
 * 此表有学号sno,姓名sname,性别sex和成绩score四个字段,主键为sno。
 */
//程序清单12-2:  CreateTableDemo.java
package jdbcodbc.access;

import java.sql.*;

public class CreateTableDemo {
	public static void main(String[] args) {
		// 声明JDBC驱动程序类型
		String JDBCDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
		// 定义JDBC的URL对象
		String conURL = "jdbc:odbc:StudentDB";
		try {// 加载JDBC驱动程序
			Class.forName(JDBCDriver);
		} catch (ClassNotFoundException e) {
			System.out.println("Class.forname:" + e.getMessage());
		}
		try {// 连接数据库URL
			Connection con = DriverManager.getConnection(conURL);
			// 建立Statement类对象
			Statement stm = con.createStatement();
			// 创建一个含有四个字段的学生表
			String query = "create table tblstudent(sno char(12) primary key not null,"
					+ "sname char(15),sex char(2),score real)";
			stm.executeUpdate(query); // 执行SQL语句
			System.out.println("数据表:tblstudent创建成功!");
			stm.close(); // 释放statement所连接的数据库及JDBC资源
			con.close();// 关闭与数据库的连接
		} catch (SQLException e) {
			System.out.println("=SQLException:" + e.getMessage());
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?