java直接通过jdbc连接操作数据库模板

简单的连接数据库的java程序,方便临时使用:

package me.muphy.tomcat.filter.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
* spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
* spring.datasource.url=jdbc:mysql://ip/hacker
* spring.datasource.password=***
* spring.datasource.username=a***z***
* <dependency>
* <groupId>mysql</groupId>
* <artifactId>mysql-connector-java</artifactId>
* <version>8.0.19</version>
* </dependency>
*/
public class RemoteHostService {
    public boolean SaveClientRequestInfo() {
try {
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
Connection conn = DriverManager.getConnection("jdbc:mysql://47.106.139.21/hacker", "azi", "***");
PreparedStatement ps = conn.prepareStatement("insert into client_visit_info(ip, visit_info) values (?,?)");
ps.setString(1, "ip");
ps.setString(2, "client_info");
ps.execute();
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}

}

 

posted @ 2019-04-28 02:39  明月心~  阅读(616)  评论(0编辑  收藏  举报