当前位置:网站首页>Simple database connection example
Simple database connection example
2022-06-12 03:13:00 【Flying little dinosaur】
# Simple database tools
public class SimpleDBDemo {
public static void main(String[] args) {
try {
// Load database driver com.mysql.cj.jdbc.Driver
String strDriver = "com.mysql.cj.jdbc.Driver";
// obtain mysql Connection address
String strURL = "jdbc:mysql://localhost:3306/mytest?&useSSL=false&serverTimezone=UTC";
// User name
String username = "root";
// User password
String password = "123456";
Class.forName(strDriver);
System.out.println("SQLServerDriver success");
// Get a data connection , Connect MySql database
Connection con = DriverManager.getConnection(strURL, username, password);
System.out.println("Connection success");
// establish statement Class object , Used to perform SQL sentence !
Statement stm = con.createStatement();
// Executes SQL sentence
String strSql = "select * from student";
// ResultSet class , Used to store the result set obtained !
ResultSet rs = stm.executeQuery(strSql);
while(rs.next()){
String s1 = rs.getString(1);
String s2 = rs.getString(2);
double d3 = rs.getDouble(3);
System.out.println(s1+"\t"+s2+"\t\t"+d3);
}
rs.close();
stm.close();
con.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
General database tools
public class JDBCUtil {
private static final String driver;
private static final String url;
private static final String userName;
private static final String password;
static {
InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
driver = properties.getProperty("driver");
url = properties.getProperty("url");
userName = properties.getProperty("username");
password = properties.getProperty("password");
}
// Get database connection
public static Connection getConnection() {
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, userName, password);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
return con;
}
// Database query , Return result set
public static ResultSet query(Connection con, PreparedStatement st, ResultSet rs, String sql
, Object[] params) throws SQLException {
st = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
if (params != null) {
for (int i = 0; i < params.length; i++) {
st.setObject(i + 1, params[i]);
}
}
rs = st.executeQuery();
return rs;
}
// Database addition, deletion and modification
public static int update(Connection con, String sql
, Object[] params, ResultSet rs, PreparedStatement st) throws SQLException {
st = con.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
st.setObject(i + 1, params[i]);
}
return st.executeUpdate();
}
// Close database connection
public static void release(Connection con, Statement st, ResultSet rs) {
boolean flag = true;
if (rs != null) {
try {
rs.close();
rs = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if (st != null) {
try {
st.close();
st = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if (con != null) {
try {
con.close();
con = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
}
}
db.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/XXX?&useSSL=false&serverTimezone=UTC
username=root
password=123456
边栏推荐
- string manipulation:
- AI interview bag | Netease mutual entertainment AI Lab artificial intelligence research engineers share on both sides
- Geforce GTX 2050/2080/3090/a6000 auto install NVIDIA graphics driver
- Function templatesfunction templates
- 2020-12-10
- 2020-12-12
- 2020-12-07
- What is the commonly heard sub table of MySQL? Why does MySQL need tables?
- golang的gin框架,各种接收参数的方式和各种绑定的区别?
- oracle之序列
猜你喜欢
随机推荐
架构入门讲解 - 谁动了我的蛋糕
2020-12-12
1 minute to understand the essential difference between low code and zero code
Laravel 8 selects JWT for interface verification
Demand and business model innovation - demand 10- observation and document review
字符串处理:
Application of ankery anti shake electric products in a chemical project in Hebei
[Bank Research Report] technology enabled retail finance carbon neutral development report (2022) - download link attached
[Business Research Report] forward looking report on urban renewal and development in China in 2021 - download link attached
Wechat applet project example - Fitness calculator
[Business Research Report] analysis report on online attention of China's e-sports industry in 2021 - download link attached
2020-12-06
Special materials | household appliances, white electricity, kitchen electricity
Requirements and business model analysis requirements 13 data modeling
如何防止商场电气火灾的发生?
[Business Research Report] 2021 global mobile game player white paper - download link attached
Introduction to architecture - who moved my cake
推荐6款办公软件,好用还免费,效率翻倍
大整数的加与乘;
[string] determine whether S2 is the rotation string 2 of S1









