⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createdatabase.java

📁 JSF 演示代码
💻 JAVA
字号:
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;

/**
 * 创建数据库表
 */
public class CreateDatabase
{

	/**
	 * 在数据库studentmanager中创建表
	 */
	public void execCreateTable() throws Exception
	{
        Statement stmt = null;
        Connection con = null;

        //得到表名与表结构信息
		String studentTableName = GetPropertyFromFile.getPropertyValue("stuenttablename");
        String courseTableName = GetPropertyFromFile.getPropertyValue("coursetablename");
        String studentTable = GetPropertyFromFile.getPropertyValue("studenttable");
        String courseTable = GetPropertyFromFile.getPropertyValue("coursetable");

        //得到数据库连接
        con = GetDBConnection.getConnection();
        try
        {
            stmt = con.createStatement();
        }
        catch (SQLException ex)
        {
            System.out.println("不能创建该连接的会话!!!");
            ex.printStackTrace();
        }

        //创建表
        createTable(studentTableName, studentTable, stmt);
        createTable(courseTableName, courseTable, stmt);
        
        //销毁连接并释放系统资源
        GetDBConnection.destroyConnection(con, stmt);
	}
	
	/**
	 * 第二次在数据库studentmanager中创建表
	 */
	public void execMuilCreateTable() throws Exception
	{
        Statement stmt = null;
        Connection con = null;

        //得到表名与表结构信息
		String studentTableName = GetPropertyFromFile.getPropertyValue("stuenttablename");
        String courseTableName = GetPropertyFromFile.getPropertyValue("coursetablename");
        String studentTable = GetPropertyFromFile.getPropertyValue("studenttable");
        String courseTable = GetPropertyFromFile.getPropertyValue("coursetable");

        //得到数据库连接
        con = GetDBConnection.getConnection();
        try
        {
            stmt = con.createStatement();
        }
        catch (SQLException ex)
        {
            System.out.println("不能创建该连接的会话!!!");
            ex.printStackTrace();
        }
        
        //删除表
        dropTable(studentTableName, stmt);
        dropTable(courseTableName, stmt);

        //创建表
        createTable(studentTableName, studentTable, stmt);
        createTable(courseTableName, courseTable, stmt);
        
        //销毁连接并释放系统资源
        GetDBConnection.destroyConnection(con, stmt);
	}
	
	private void createTable(String tableName,String tableFrame, Statement stmt) throws Exception
    {
    	System.out.println("the table name is " + tableName);

    	String command = "CREATE TABLE " + tableName + "(" +tableFrame + ")";
        try
        {
            stmt.executeUpdate(command);
        }
        catch (SQLException ex)
        {
            System.out.println("创建表及表结构错误");
            ex.printStackTrace();
            throw new Exception("数据库初始化失败");
        }
    }
    
    private void dropTable(String tableName, Statement stmt) throws Exception
    {
    	System.out.println("drop the table " + tableName);

    	String command = "drop TABLE " + tableName;
        try
        {
            stmt.executeUpdate(command);
        }
        catch (SQLException ex)
        {
            System.out.println("删除表错误");
            ex.printStackTrace();
            throw new Exception("数据库初始化失败");
        }
    }
}

⌨️ 快捷键说明

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