java0016——JDBC使用executeupdate执行DDl和DML语句

  • 成品类\
package ConnectMySql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnMySQL01  {
    private  String driver;
    private  String url;
    private String user;
    private  String password;
    Connection connection;
    Statement statement;
    /*public  void initprograme(String prarmFile)throws Exception{
//String getProperty(String key)
// 用指定的键在此属性列表中搜索属性。
        Properties properties=new Properties();
//        void load(InputStream streamIn) throws IOException
//        从输入流中读取属性列表(键和元素对)。
        properties.load(new FileInputStream(prarmFile));
        driver = properties.getProperty("driver");
        url=properties.getProperty("url");
        user=properties.getProperty("user");
        password = properties.getProperty("password");
    }*/
    public ConnMySQL01(String driver, String url, String user, String password) {
        this.driver = driver;
        this.url = url;
        this.user = user;
        this.password = password;
    }
    public void CreateTable(String sql)throws Exception{
    //    public int addData(String sql)throws Exception{
        try {
            Class.forName(driver);/*加载驱动*/
            connection= DriverManager.getConnection(url,user,password);/*连接数据库*/
            statement =connection.createStatement();/*创建一个对象用于执行SQL语句*/
//            使用executeUpdate方法的DML与DDL操作基本相似,DDL返回零,DML返回受影响的记录条数
            statement.executeUpdate(sql);/*执行DDL 创建列表*/             return //statement.executeUpdate(sql);/*添加数据*/
        } catch (ClassNotFoundException e) {
            System.out.println("驱动加载失败");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("数据库连接失败");
            e.printStackTrace();
        } finally {
            if(statement!=null){
                statement.close();
            }
            if (connection != null) {
                statement.close();
            }
        }
    }
}
  • 测试类
package ConnectMySql;

public class ConnMySQL01Demo {
    public  static void main(String[] args) throws Exception{
        String driver = "com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT";
        String username = "root";
        String password = "123456";
        ConnMySQL01 conn = new ConnMySQL01(driver,url,username,password);/*连接数据库*/
        String SQL = "create table jdbc_demo(" +
                "name varchar(255)," +
                "id int," +
                "love text);";
        conn.CreateTable(SQL);
        System.out.println("------建表成功------");
/*          String sql="insert into jdbc_demo values" +
                "('dan',1,'玩游戏'),('Tina',2,'学习');";
        int reslut=conn.CreateTable(sql);
        System.out.println("------共有" +
                reslut+"条信息被改变------");
   */
    }
}

  • 数据库中的效果
+----------------+
| Tables_in_test |
+----------------+
| firstdemo      |
| jdbc_demo      |   /*新增的表*/
+----------------+
//表中的数据类型
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name  | varchar(255) | YES  |     | NULL    |       |
| id    | int(11)      | YES  |     | NULL    |       |
| love  | text         | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
+------+------+--------+
| name | id   | love   |
+------+------+--------+
| dan  |    1 | 玩游戏 |
| Tina |    2 | 学习   |
| dan  |    1 | 玩游戏 |
| Tina |    2 | 学习   |
+------+------+--------+
//执行了两次,手速有点慢

猜你喜欢

转载自blog.csdn.net/qq_41448891/article/details/82876190