**用java连接数据库**

1.将mysql的jdbc驱动加载到内存中

指定需要连接的数据库地址,用户名和密码

String url = "jdbc:mysql://127.0.0.1:3306/j175";
String user = "root";
String password = "123456";

2.获取连接

conn = DriverManager.getConnection(url, user, password);

3.通过连接创建Statement对象

stmt = conn.createStatement();

4.执行数据库语句 DML

jdbc中增删改都是executeUpdate方法

这个方法会返回一个int类型的值

对应就是几行数据受影响

插入语句 int n = stmt.executeUpdate("insert into t_user(username,userpwd,tel,address,sex)values('222','222','222','112221','男')");

删除语句 int n = stmt.executeUpdate("delete from t_user where sex='男'");

修改语句 int n = stmt.executeUpdate("update t_user set sex='女',address='绵阳',tel='13888888888' where u_pk_id=1");

5.关闭数据库

posted @ 2019-12-23 17:25  现在不晓得  阅读(196)  评论(0)    收藏  举报