IDEA连接mysql数据库如何配置
2021-08-31 本文已影响0人
司马青藤

如图所示,点击右侧边栏Database,点加号,Data Source,MySQL,弹出下图所示界面。

如上图所示,填写用户名,密码,数据库名,点击测试链接,连接数据库成功的话就可以了。
这时已经可以查看数据库信息了:

下面用代码调用sql语句去操作一下数据库:
public class JdbcDemo1 {
public static void main(String[] args) {
//注册驱动
Connection conn =null;
Statement statement =null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String sql ="insert into account values(3,'赵六',234999)";
conn = DriverManager.getConnection("jdbc:mysql:///db1","root","xxxxxx");//数据库,用户名,密码
statement = conn.createStatement();
int count = statement.executeUpdate(sql);
System.out.println(count);
if (count >0) {
System.out.println("执行成功");
}else {
System.out.println("执行失败");
}
}catch (ClassNotFoundException|SQLException e) {
e.printStackTrace();
}finally {
try {
if (statement !=null) statement.close();
if (conn !=null) conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
}
}
执行完,查看数据库验证,发现数据插入成功!