java 连接mysql数据库并创建表
时间:2021-07-01 10:21:17
帮助过:9人阅读
import java.sql.*
;
2 public class Linksql {
3
4 //我么要执行创建表的DDl语句
5 static String creatsql = "CREATE TABLE pepole("
6 + "name varchar(10) not null,"
7 + "age int(4) not null"
8 + ")charset=utf8;"
;
9
10 final static String JDBC_DRIVER = "com.mysql.jdbc.Driver"
;
11 //指定连接数据库的url
12 final static String DB_URL = "jdbc:mysql://localhost/student"
;
13 //mysql用户名
14 final static String name = "root"
;
15 //mysql密码
16 final static String pwd = "pwd"
;
17 public static void main(String[] args)
18 {
19 Connection conn =
null;
20 Statement stmt =
null;
21 try
22 {
23 //注册jdbc驱动
24 Class.forName(JDBC_DRIVER);
25 //打开连接
26 System.out.println("//连接数据库"
);
27 conn =
DriverManager.getConnection(DB_URL,name,pwd);
28 //执行创建表
29 System.out.println("//创建表"
);
30 stmt =
conn.createStatement();
31 if(0 ==
stmt.executeLargeUpdate(creatsql))
32 {
33 System.out.println("成功创建表!"
);
34 }
35 else
36 {
37 System.out.println("创建表失败!"
);
38 }
39 //
40 stmt.close();
41 conn.close();
42 System.out.println("//关闭资源"
);
43 }
44 catch(Exception e)
45 {
46 System.out.println("创建表失败!"
);
47 e.printStackTrace();
48 }
49 }
50 }
51
java 连接mysql数据库并创建表
标签:nec summary https tor 文档 color charset imp arch