码迷,mamicode.com
首页 > 数据库 > 详细

Statement执行静态SQL语句

时间:2017-05-23 01:20:40      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:mysq   驱动   statement   tab   ring   com   out   table   name   

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

public class Demo1 {
    private String url = "jdbc:mysql://localhost:3306/tableName";
    private String user = "root";
    private String password = "root";
    @Test
    public void test1(){
        Connection conn = null;
        Statement stmt = null;
        try{
            //1、注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            
            //2、创建连接对象
            conn = DriverManager.getConnection(url, user, password);
            
            //3、创建Statement对象
            stmt = conn.createStatement();
            
            //4、准备sql语句
            String sql = "CREATE TRABLE student(id PRIMARY KEY AUTO_INCREMENT,name VARCHAR(20), gender(2))";
            
            //5、发送sql语句、执行sql语句
            int count = stmt.executeUpdate(sql);
            
            //5、输出
            System.out.println("影响了" + count + "行!");
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally{
            //7、关闭连接(顺序:后打开的先关闭)
            if(stmt != null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
            
        }
    }
}

 

Statement执行静态SQL语句

标签:mysq   驱动   statement   tab   ring   com   out   table   name   

原文地址:http://www.cnblogs.com/StanLong/p/6892184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!