虽然简单,但是有时候只能留个印象。
public class JdbcUtil { static String url = null; static String username = null; static String password = null; static String driverClass = null; static { try { Properties properties = new Properties(); InputStream in = JdbcUtil.class .getResourceAsStream("/jdbc.properties"); properties.load(in); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); driverClass = properties.getProperty("driver"); System.out.println(password); Class.forName(driverClass); } catch (Exception e) { e.printStackTrace(); System.out.println("驱动加载失败!"); throw new RuntimeException(e); } } public static Connection getConnection() { try { Connection conn = DriverManager.getConnection(url, username, password); return conn; } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } public static void close(Connection conn, Statement st, ResultSet rs) { if (conn != null) { try { conn.close(); } catch (Exception e) { throw new RuntimeException(e); } } if (st != null) { try { st.close(); } catch (Exception e) { throw new RuntimeException(e); } } if (rs != null) { try { rs.close(); } catch (Exception e) { throw new RuntimeException(e); } } } public static void close(Connection conn, Statement st) { if (conn != null) { try { conn.close(); } catch (Exception e) { throw new RuntimeException(e); } } if (st != null) { try { st.close(); } catch (Exception e) { throw new RuntimeException(e); } } } /* * 释放资源 */ public static void releaseResources(ResultSet resultSet, Statement statement, Connection connection) { try { if (resultSet != null) resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } finally { resultSet = null; try { if (statement != null) statement.close(); } catch (SQLException e) { e.printStackTrace(); } finally { statement = null; try { if (connection != null) connection.close(); } catch (SQLException e) { e.printStackTrace(); } finally { connection = null; } } } } }
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import bean.QuackResults; //存入数据库新增加功能 /** * 2017/10/21 * @author lemo * */ public class DbExcute { private Connection connection; private Statement statement; private ResultSet resultSet; // 更新操作 public void update(String sql) { try { connection = JdbcUtil.getConnection(); statement = connection.createStatement(); // 可执行创建、修改、删除表,添加、删除、修改元组以及查询sql语句 boolean num=statement.execute(sql); System.out.println(num); } catch (SQLException e) { e.printStackTrace(); } finally { JdbcUtil.close(connection, (com.mysql.jdbc.Statement) statement, resultSet); } } // 查询操作 public ResultSet Query(String sql) { try { connection = JdbcUtil.getConnection(); statement = connection.createStatement(); resultSet = statement.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } finally { JdbcUtil.releaseResources(resultSet, statement, connection); } return resultSet; } // 添加操作 public void addElement(String sql) { update(sql); } public void addElement(QuackResults aQuackResults) { /*String sqlStr = "select* from users where username=? and password=?"; connection = JdbcUtil.getConnection(); PreparedStatement aStatement=null; try { aStatement=connection.prepareStatement(sqlStr); aStatement.setString(1,"zhansgan"); aStatement.setString(2, "root"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ String sqlStr = "insert into mine_quack_results values(null,?,?,?,?,?)"; connection = JdbcUtil.getConnection(); PreparedStatement aStatement=null; try { aStatement=connection.prepareStatement(sqlStr); aStatement.setDouble(1,aQuackResults.getxData()); aStatement.setDouble(2, aQuackResults.getyData()); aStatement.setDouble(3, aQuackResults.getzData()); aStatement.setTimestamp(4,aQuackResults.getQuackTime()); aStatement.setDouble(5, aQuackResults.getQuackGrade()); System.out.println(aStatement.execute()); connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 删除操作 public void removeElement(String sql) { update(sql); } // 创建一个表 public void createTable(String sql) { update(sql); } // 删除一个表 public void dropTable(String sql) { update(sql); } }
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/ks?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true username=root password=root #定义初始连接数 initialSize=0 #定义最大连接数 maxActive=20 #定义最大空闲 maxIdle=20 #定义最小空闲 minIdle=1 #定义最长等待时间 maxWait=60000