时间:2021-07-01 10:21:17 帮助过:14人阅读
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
jdbc链接数据库
标签:close oid 资源 tco getc cal log select getx