JDBC 创建、删除表范例

jdbc 创建、删除表范例

在本教程将演示如何在 jdbc 应用程序中创建 和 删除表。

删除表后,表中的所有内容都将丢失,所以必须在继续删除表之前作出明确的决定。

 

1. 创建表范例

复制以下示例代码保存到文件:createtable.java中,然后编译并运行如下:

//step 1. import required packages

import java.sql.*;

public class createtable {
    // jdbc driver name and database url
    static final string jdbc_driver = "com.mysql.jdbc.driver";  
    static final string db_url = "jdbc:mysql://localhost/jdbc_db";

    //  database credentials
    static final string user = "root";
    static final string pass = "123456";

    public static void main(string[] args) {
        connection conn = null;
        statement stmt = null;
        try{
            //step 2: register jdbc driver
            class.forname("com.mysql.jdbc.driver");

            //step 3: open a connection
            system.out.println("connecting to a selected database...");
            conn = drivermanager.getconnection(db_url, user, pass);
            system.out.println("connected database successfully...");

            //step 4: execute a query
            system.out.println("creating table in given database...");
            stmt = conn.createstatement();

            string sql = "create table student " +
                        "(id integer not null, " +
                        " first varchar(255), " + 
                        " last varchar(255), " + 
                        " age integer, " + 
                        " primary key ( id ))"; 

            stmt.executeupdate(sql);
            system.out.println("created table in given database...");
        }catch(sqlexception se){
            //handle errors for jdbc
            se.printstacktrace();
        }catch(exception e){
            //handle errors for class.forname
            e.printstacktrace();
        }finally{
            //finally block used to close resources
            try{
                if(stmt!=null)
                conn.close();
            }catch(sqlexception se){
            }// do nothing
            try{
                if(conn!=null)
                conn.close();
            }catch(sqlexception se){
                se.printstacktrace();
            }//end finally try
        }//end try
        system.out.println("goodbye!");
    }//end main
}   

编译上面代码,如下 -

f:\worksp\jdbc> javac -djava.ext.dirs=f:\worksp\jdbc\libs createtable.java

执行上面代码,如下 -

f:\worksp\jdbc>java -djava.ext.dirs=f:\worksp\jdbc\libs createtable
connecting to a selected database...
thu jun 01 22:17:34 cst 2017 warn: establishing ssl connection without server's identity verification is not recommended. according to mysql 5.5.45+, 5.6.26+ and 5.7.6+ requirements ssl connection must be established by default if explicit option isn't set. for compliance with existing applications not using ssl the verifyservercertificate property is set to 'false'. you need either to explicitly disable ssl by setting usessl=false, or set usessl=true and provide truststore for server certificate verification.
connected database successfully...
creating table in given database...
created table in given database...
goodbye!

 

2. 删除表范例

复制以下示例代码保存到文件:droptable.java中,然后编译并运行如下:

//step 1. import required packages
import java.sql.*;

public class droptable {
    // jdbc driver name and database url
    static final string jdbc_driver = "com.mysql.jdbc.driver";  
    static final string db_url = "jdbc:mysql://localhost/jdbc_db";

    //  database credentials
    static final string user = "root";
    static final string pass = "123456";

    public static void main(string[] args) {
        connection conn = null;
        statement stmt = null;
        try{
            //step 2: register jdbc driver
            class.forname("com.mysql.jdbc.driver");

            //step 3: open a connection
            system.out.println("connecting to a selected database...");
            conn = drivermanager.getconnection(db_url, user, pass);
            system.out.println("connected database successfully...");

            //step 4: execute a query
            system.out.println("deleting table in given database...");
            stmt = conn.createstatement();

            string sql = "drop table student ";

            stmt.executeupdate(sql);
            system.out.println("table  deleted in given database...");
        }catch(sqlexception se){
            //handle errors for jdbc
            se.printstacktrace();
        }catch(exception e){
            //handle errors for class.forname
            e.printstacktrace();
        }finally{
            //finally block used to close resources
            try{
                if(stmt!=null)
                conn.close();
            }catch(sqlexception se){
            }// do nothing
            try{
                if(conn!=null)
                conn.close();
            }catch(sqlexception se){
                se.printstacktrace();
            }//end finally try
        }//end try
        system.out.println("goodbye!");
    }//end main
}   

编译上面代码,如下 -

f:\worksp\jdbc> javac -djava.ext.dirs=f:\worksp\jdbc\libs droptable.java

执行上面代码,如下 -

f:\worksp\jdbc>java -djava.ext.dirs=f:\worksp\jdbc\libs droptable

connecting to a selected database...
thu jun 01 22:39:01 cst 2017 warn: establishing ssl connection without server's identity verification is not recommended. according to mysql 5.5.45+, 5.6.26+ and 5.7.6+ requirements ssl connection must be established by default if explicit option isn't set. for compliance with existing applications not using ssl the verifyservercertificate property is set to 'false'. you need either to explicitly disable ssl by setting usessl=false, or set usessl=true and provide truststore for server certificate verification.
connected database successfully...
deleting table in given database...
table  deleted in given database...
goodbye!

在执行上面语句后,数据库 jdbc_db 中的 student 表将会被成功地删除了。

下一节:jdbc 插入数据范例

jdbc 教程

相关文章