数据库的操作MySQL
增删查改
成都创新互联是专业的临潼网站建设公司,临潼接单;提供成都网站建设、成都网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行临潼网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
mysql> show databases;
数据库的展示
mysql> use java12
使用数据库
mysql> drop table person;
删除表格
Query OK, 0 rows affected (0.01 sec)
mysql> create table person(age int(4),name varchar(20),telNum varchar(14));
Query OK, 0 rows affected (0.01 sec)
创建表格名为person
mysql> insert into person(age,name,telNum) values(20,"AA","13603711234");
Query OK, 1 row affected (0.02 sec)
插入数据
mysql> update person set telNum = "13838260132" where name = "CC";
Query OK, 1 row affected (0.00 sec)
更新数据
mysql> delete from person where age = 20;
Query OK, 1 row affected (0.00 sec)
表格数据的删除
//更新数据表中文报错,因为创建数据库的时候并没有指定字符编码
mysql> update person set name ="张三" where telNum = "18603710987";
ERROR 1366 (HY000): Incorrect string value: '\xD5\xC5\xC8\xFD' for column 'name'
at row 1
创建一个可以存储汉字的数据库
//插入多条记录报错
mysql> insert into person(age,name,telNum) values(22,"DD","110") (23,"EE","120")
;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '(23,"
EE","120")' at line 1
Java与数据库的连接
首先建立驱动:
DriverManager.registerDriver(new Driver());
-----然后建立连接
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/computer","root","root");
建立SQL语句
Statement statement=(Statement) conn.createStatement();
ResultSet resultSet=statement.executeQuery("select * from user");
数据的处理
遍历数据并且进行接收
while(resultSet.next()){
String name=resultSet.getString("name");
double heigh=resultSet.getDouble("heigh");
String sex=resultSet.getString("sex");
int weight=resultSet.getInt("weight");
System.out.println("姓名为"+name+"身高为"+heigh+"性别为"+sex);
}
创建四个接受的对象,进行数据的接受。
名称栏目:Java基础学习之数据库连接
当前URL:
http://cxhlcq.com/article/jjoooe.html