parent
98de7ea0d3
commit
66d631097f
@ -0,0 +1,5 @@
|
||||
.classpath
|
||||
.project
|
||||
.metadata
|
||||
.settings
|
||||
build
|
@ -1,4 +0,0 @@
|
||||
driver=com.mysql.jdbc.Driver
|
||||
url=jdbc:mysql://localhost:3306/library?useSSl=true&userUnicode=true&characterEncoding=utf-8
|
||||
username=root
|
||||
password=root
|
Binary file not shown.
@ -0,0 +1,55 @@
|
||||
package javabean;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class JDBCBean {
|
||||
private static String driver = "com.mysql.jdbc.Driver";
|
||||
private static String url = "jdbc:mysql://localhost:3306/library?useSSl=true&useUnicode=true&characterEncoding=utf-8";
|
||||
private static String username = "root";
|
||||
private static String password = "root";
|
||||
private Connection conn = null;
|
||||
private Statement stmt = null;
|
||||
|
||||
public JDBCBean() {
|
||||
try {
|
||||
Class.forName(driver);
|
||||
conn = DriverManager.getConnection(url, username, password);
|
||||
stmt = conn.createStatement();
|
||||
System.out.println("同数据库建立连接!");
|
||||
} catch (Exception ex) {
|
||||
System.out.println("无法同数据库建立连接!");
|
||||
}
|
||||
}
|
||||
|
||||
public int executeUpdate(String s) {
|
||||
int result = 0;
|
||||
try {
|
||||
System.out.println(s + "------" + stmt + "-----");
|
||||
result = stmt.executeUpdate(s);
|
||||
} catch (Exception e) {
|
||||
System.out.println("执行更新错误!");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public ResultSet executeQuery(String s) {
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
rs = stmt.executeQuery(s);
|
||||
} catch (Exception e) {
|
||||
System.out.println("执行查询错误!");
|
||||
}
|
||||
return rs;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
stmt.close();
|
||||
conn.close();
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue