Document

 目录:


 1 package com.yanlong.jdbc.statement;
 2 
 3 import java.sql.Connection;
 4 import java.sql.Driver;
 5 import java.sql.DriverManager;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 import java.util.Properties;
 9 
10 import org.junit.Test;
11 
12 
13 /** 
14 * 类说明 :
15 * @author 作者 :chenyanlong
16 * @version 创建时间:2017年11月29日 
17 */
18 public class Demo {
19 
20     private static String url="jdbc:mysql://localhost:3306/test";
21     private static String  user="root";
22     private static String password="123456";
23     
24     public static void main(String[] args) {
25     
26         Connection conn=null;
27         Statement st=null;
28         try {
29             //1.加载驱动
30             Class.forName("com.mysql.jdbc.Driver");
31             
32             //2.链接数据库
33             conn=DriverManager.getConnection(url, user, password);
34 
35             //3.java程序与数据库连接
36             st=conn.createStatement();
37             
38             //4.数据库语句
39             String sql="create table student(id int primary key auto_increment,name varchar(20),gender varchar(2));";
40             
41             //5.发送sql语句
42             int count=st.executeUpdate(sql);
43             
44             //6.输出语句
45             System.out.println("影响了"+count+"行");
46         } catch (Exception e) {
47             // TODO Auto-generated catch block
48             e.printStackTrace();
49         }finally{
50             try {
51                 if(st!=null){
52                     st.close();
53                 }
54                 if(conn!=null){
55                     conn.close();
56                 }
57                 
58             } catch (SQLException e) {
59                 // TODO Auto-generated catch block
60                 e.printStackTrace();
61             }
62         }
63         
64     }
65 
66 }
View Code
 1 package com.yanlong.jdbc.statement;
 2 
 3 import java.sql.Connection;
 4 import java.sql.Driver;
 5 import java.sql.DriverManager;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 import java.util.Properties;
 9 
10 import org.junit.Test;
11 
12 
13 /** 
14 * 类说明 :
15 * @author 作者 :chenyanlong
16 * @version 创建时间:2017年11月29日 
17 */
18 public class Demo2 {
19 
20     private static String url="jdbc:mysql://localhost:3306/test";
21     private static String user="root";
22     private static String password="123456";
23     
24     static Connection conn=null;
25     static Statement st=null;
26     
27     public static void main(String[] args) throws SQLException {
28     
29         try {
30             //1.加载驱动
31             Class.forName("com.mysql.jdbc.Driver");
32             
33             //2.连接数据库
34             Connection conn=DriverManager.getConnection(url, user, password);
35             
36             //3.sql命令
37             String sql="insert into student values(1001,'王五','男')";
38             
39             //4.数据库的运行
40             Statement st=conn.createStatement();
41             
42             int count=st.executeUpdate(sql);
43             
44             System.out.println("插入数据成功:"+count);
45             
46         } catch (Exception e) {
47             // TODO Auto-generated catch block
48             e.printStackTrace();
49         }finally{
50             if(st!=null){
51                 st.close();
52             }
53             if(conn!=null){
54                 conn.close();
55             }
56         }
57         
58     }
59 }
View Code
原文地址:https://www.cnblogs.com/chenyanlong/p/7920679.html