输入项目名称
空项目
依次点击 File -> Project Structure(快捷键 Ctrl + Alt + Shift + s),点击Project Structure界面左侧的“Modules”如图:
在 【Dependencies】 标签界面下,点击左边的 “+”号,选择第一个选项“JARs or directories…”,选择相应的jar包,点“OK”,jar包添加成功
获取驱动包:
链接:https://pan.baidu.com/s/1VQvpbcsYska_i5JVOCMxqg
提取码:yyds
【com.mysql.jdbc.Driver】
private static final String URL = "jdbc:mysql://localhost:3306/test?useUnicode&CharacterEncoding=utf-8";
private static final String USE = "root";
private static final String PASSWORD = "root";
package com.item.jdbc;
/**
* @author 为一道彩虹
*/
public class FactoryDB
{
private static final String URL = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
private static final String USER = "root";
private static final String PASSWORD = "root";
}
package com.item.jdbc;
import java.sql.*;
/**
* @author 为一道彩虹
*/
public class FactoryDB
{
private static final String URL = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
private static final String USER = "root";
private static final String PASSWORD = "root";
//1.加载驱动
static
{
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
// 2.获取数据库连接
public static Connection getConnection()
{
try
{
return DriverManager.getConnection(URL, USER, PASSWORD);
}
catch (SQLException throwables)
{
throwables.printStackTrace();
}
return null;
}
// 3.关闭资源
public static void Close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet)
{
try
{
if (resultSet != null)
{
resultSet.close();
}
if (preparedStatement != null)
{
preparedStatement.close();
}
if (connection != null)
{
connection.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
CREATE TABLE `userinfo` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`createDate` datetime NOT NULL,
`userName` varchar(32) NOT NULL,
`introduce` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO userinfo VALUES (0,NOW(),'admin','管理员很厉害');
INSERT INTO userinfo VALUES (0,NOW(),'zhangsan','张三不认李四');
INSERT INTO userinfo VALUES (0,NOW(),'lisi','李四都认识');
INSERT INTO userinfo VALUES (0,NOW(),'wangwu','大刀王五');
INSERT INTO userinfo VALUES (0,NOW(),'zhaoliu','赵家老六');
INSERT INTO userinfo VALUES (0,NOW(),'ruanxiaoqi','打鱼的阮小七');
INSERT INTO userinfo VALUES (0,NOW(),'baxianwang','贤王一名');
select * from userinfo;
时间我没随机给,比较麻烦,自己改一下也行的哦。
包名:【com.item.demo】
类名:【Action】
package com.item.demo;
import com.item.jdbc.FactoryDB;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author 为一道彩虹
*/
public class Action
{
public static void main(String[] args)
{
Connection conn = FactoryDB.GetConnection();
PreparedStatement pre = null;
ResultSet res = null;
try
{
pre = conn.prepareStatement("select * from userinfo");
res = pre.executeQuery();
while (res.next())
{
System.out.print("id:"+res.getInt(1)+"\t");
System.out.print("createDate:"+res.getTime(2)+"\t");
System.out.print("userName:"+res.getString(3)+"\t");
System.out.println("introduce:"+res.getString(4));
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
FactoryDB.Close(conn,pre,res);
}
}
}
查询成功:
先赞后看,养成习惯!!!^ _ ^ ❤️ ❤️ ❤️
码字不易,大家的支持就是我的坚持下去的动力。点赞后不要忘了关注我哦!
更多【java-JavaWeb课程复习资料——idea创建JDBC】相关视频教程:www.yxfzedu.com