• java编写创建数据库和表的程序


    本文示例可见一斑了,主要是通过Java对SQL语句进行操作,和普通的增删改查的原理是一样的:

    import java.sql.*;  
       
    public class Test  
    {  
        public static void main(String[] args) throws Exception  
        {  
            Class.forName("com.mysql.jdbc.Driver");  
               
            //一开始必须填一个已经存在的数据库  
            String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";     
            Connection conn = DriverManager.getConnection(url, "root", "123456");  
            Statement stat = conn.createStatement();  
               
            //创建数据库hello  
            stat.executeUpdate("create database hello");  
               
            //打开创建的数据库  
            stat.close();  
            conn.close();  
            url = "jdbc:mysql://localhost:3306/hello?useUnicode=true&characterEncoding=utf-8";  
            conn = DriverManager.getConnection(url, "root", "123456");  
            stat = conn.createStatement();  
               
            //创建表test  
            stat.executeUpdate("create table test(id int, name varchar(80))");  
               
            //添加数据  
            stat.executeUpdate("insert into test values(1, '张三')");  
            stat.executeUpdate("insert into test values(2, '李四')");  
               
            //查询数据  
            ResultSet result = stat.executeQuery("select * from test");  
            while (result.next())  
            {  
                System.out.println(result.getInt("id") + " " + result.getString("name"));  
            }  
               
            //关闭数据库  
            result.close();  
            stat.close();  
            conn.close();  
        }  
    }

    注:参考链接:http://blog.csdn.net/sd4015700/article/details/39668583

  • 相关阅读:
    Periodic Strings ( UVA
    Digit Counting ( UVA
    Molar mass ( UVA
    Score ( UVA
    Let the Balloon Rise (STL)
    Tree Recovery (STL)
    安卓simpleadapter问题
    安卓活跃手指问题
    python 问题记录
    设计模式笔记
  • 原文地址:https://www.cnblogs.com/kings-9/p/7629232.html
Copyright © 2020-2023  润新知