您的位置:首页 > 数据库

使用IDEA的jdbc连接数据库,增删改查数据。(后台输入数据在最后)

2019-09-09 13:47 1381 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/weixin_44363242/article/details/100659819

先使用SQLyog创建一个数据库和数据表并添加一条记录。我创建的数据库叫zjgm,数据表叫stu并添加了一条id为1,name为yxd,age为18的数据。

然后打开IDEA,新建一个项目。我的项目叫jdbc。

然后在src下面创建包。我的包是com.zhongruan(就是创建了两个包,zhongruan在com的目录下)

然后在zhongruan下面创建一个Java Class。我命名为Test。
然后我们需要导入一个mysql-connector-java的架包。在jdbc下面创建一个包,我命名为lib,然后把下载好的mysql-connector-java的架包,复制粘贴进去。
然后右键点击这个架包点击 Add as library,解析这个架包。

然后在Test里面输入代码

package com.zhongruan;
import java.sql.*;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建链接
Connection connection=DriverManager.getConnection("jdbc:mysql://localhost/zjgm?user=root&password=123456&characterEncoding=utf-8&useSSL=true");
System.out.println("创建连接成功");

}
}

然后运行,成功连接数据库,会出现 创建连接成功。

然后在下面继续补充一段查询数据的代码。

//3.写sql
String sql="select * from stu";
//4.得到statement对象,
PreparedStatement statement=connection.prepareStatement(sql);
//5.执行sql
ResultSet resultSet=statement.executeQuery();
//6.处理结果集
while (resultSet.next()){
int id=resultSet.getInt(1);
String name=resultSet.getString(2);
int age = resultSet.getInt(3);
System.out.println("id="+id+"\nname="+name+"\nage="+age);
}
//7.关闭资源
connection.close();
statement.close();
resultSet.close();


删除数据的代码为

//3.写sql
String sql="delete from stu where name='yxd' ";
//4.得到statement对象,
PreparedStatement statement=connection.prepareStatement(sql);
//5.执行sql
statement.executeUpdate();
System.out.println("删除成功");
//6.关闭资源
connection.close();
statement.close();

增加数据的代码为

//3.写sql
String sql="insert into stu(id,name,age) value('2','yxd','18') ";
//4.得到statement对象,
PreparedStatement statement=connection.prepareStatement(sql);
//5.执行sql
statement.executeUpdate();
System.out.println("增加数据成功");
//6.关闭资源
connection.close();
statement.close();

修改数据的代码为

//3.写sql
String sql="update stu set name='xxx' where id='1'";
//4.得到statement对象,
PreparedStatement statement=connection.prepareStatement(sql);
//5.执行sql
statement.executeUpdate();
System.out.println("修改数据成功");
//6.关闭资源
connection.close();
statement.close();

补充

如果要从后台输入数据则需要导入java.util.*,并使用Scanner创建一个输入对象。添加数据的代码修改为

package com.zhongruan;
import java.sql.*;
import java.util.*;
public class Test_insert{
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建链接
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost/zjgm?user=root&password=123456&characterEncoding=utf-8&useSSL=true");
System.out.println("创建连接成功");
//3.写sql
Scanner s=new Scanner(System.in);
System.out.println("请输入你的学号:");
int id=s.nextInt();
String xxx=s.nextLine();//这里用来吃回车,回车也算字符
System.out.println("请输入你的姓名:");
String name=s.nextLine();
System.out.println("请输入你的年龄:");
int age=s.nextInt();
s.close();
//System.out.println(id+name+age);
String sql="insert into stu(id,name,age) value('"+id+"','"+name+"' ,'"+age+"') ";
//4.得到statement对象,
PreparedStatement statement=connection.prepareStatement(sql);
//5.执行sql
statement.executeUpdate();
System.out.println("增加数据成功");
//6.关闭资源
connection.close();
statement.close();

}
}

删除,修改的代码也做类似的修改。

也可以把连接的代码和关闭资源的代码重新写成一个类,

package com.zhongruan;
import java.sql.*;
public class DBUtil {
public  static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost/zjgm?user=root&password=123456&characterEncoding=utf-8&useSSL=true");
return connection;
}
public static void closee(Statement statement, Connection connection,ResultSet resultSet) throws SQLException {
statement.close();
connection.close();
if(resultSet!=null){
resultSet.close();
}
}
}

之后要连接数据库和关闭资源就可以直接使用这两个静态方法。例如增加数据的代码

package com.zhongruan;
import java.sql.*;
import java.util.*;
public class Test_insert{
public static void main(String[] args) throws ClassNotFoundException, SQLException {
DBUtil dbUtil=new DBUtil();
Connection connection=dbUtil.getConnection();
System.out.println("创建连接成功");
//3.写sql
Scanner s=new Scanner(System.in);
System.out.println("请输入你的学号:");
int id=s.nextInt();
String xxx=s.nextLine();//这里用来吃回车,回车也算字符
System.out.println("请输入你的姓名:");
String name=s.nextLine();
System.out.println("请输入你的年龄:");
int age=s.nextInt();
s.close();
//System.out.println(id+name+age);
String sql="insert into stu(id,name,age) value('"+id+"','"+name+"' ,'"+age+"') ";
//4.得到statement对象,
PreparedStatement statement=connection.prepareStatement(sql);
//5.执行sql
statement.executeUpdate();
System.out.println("增加数据成功");
//6.关闭资源
dbUtil.closee(statement,connection,null);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐