本帖最后由 杨兴庭 于 2013-5-17 11:58 编辑
- //写了段向MySQL数据库demo数据库中student表中添加数据的代码,测试类测试没有报错,,但是MySQL中并没有添加上数据,代码如下,看是不是哪里写错,,检查一天了都没发现问题,麻烦大家看下,能找出问题提供20金币感谢。。有帮助的建议提供3金币感谢,谢谢大家了
- /**
- *
- * 学生类
- *
- */
- public class Student {
- /*声明属性*/
- private String name;
- private String age;
- private String sex;
-
-
- /*getter,setter方法*/
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
-
- /*构造方法*/
- public Student(String name, String age, String sex) {
- super();
- this.name = name;
- this.age = age;
- this.sex = sex;
- }
-
-
-
-
- }
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- /**
- *
- * 封装连接数据库方法类
- *
- */
- public class BaseDAO {
-
- /*数据源设置*/
- public static final String DRIVER="com.mysql.jdbc.Driver";
- public static final String URL="jdbc:mysql://localhost:3306/demo";
- public static final String USER="root";
- public static final String PWD="123123";
-
- /*jdbc对象*/
- protected Connection conn=null;
- protected PreparedStatement pstmt=null;
- protected ResultSet rs=null;
-
-
- /*数据库连接方法*/
- protected Connection getConn(){
-
- try {
- Class.forName(DRIVER);
- conn=DriverManager.getConnection(URL,USER,PWD);
- } catch (ClassNotFoundException e) {
- System.out.println("没有找到相关驱动。。。。。。");
- e.printStackTrace();
- } catch (SQLException e) {
- System.out.println("连接数据库出错。。。");
- e.printStackTrace();
- }
-
- return conn;
-
- }
-
-
- /**
- * 释放连接方法
- */
- protected void releaseALL(Connection conn,PreparedStatement pstmt,ResultSet rs){
-
- if(rs!=null){
- try {
- rs.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if(pstmt!=null){
- try {
- pstmt.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if(conn!=null){
- try {
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
-
-
- }
- /**
- *
- * 封装对数据库操作类
- *
- */
- public class MangerDAO extends BaseDAO {
-
- /**
- * add方法
- */
- public int addStu(Student stu){
- int result=0;
- String inserSql="insert into student(name,age,sex)values(?,?,?)";
-
-
- try {
- conn=getConn();
- pstmt=conn.prepareStatement(inserSql);
-
- pstmt.setString(1, stu.getName()) ;
- pstmt.setString(2, stu.getAge());
- pstmt.setString(3, stu.getSex());
-
-
-
-
-
-
- } catch (SQLException e) {
- System.out.println("添加方法出错");
- e.printStackTrace();
- }finally{
-
- releaseALL(conn,pstmt,null);
- }
-
-
-
- return result;
-
- }
-
- }
- public class test {
- /**
- * 测试类
- */
- public static void main(String[] args) {
- /*声明张三对象*/
- Student zhangsan=new Student("张三","21","男");
- /*调用MangerDAO内add方法*/
- MangerDAO test=new MangerDAO();
- test.addStu(zhangsan);
-
-
-
- }
- }
复制代码 |
-
a1.jpg
(113.44 KB, 下载次数: 0)
-
a2.jpg
(79.32 KB, 下载次数: 0)
test类运行后没有添加上数据
-
a3.jpg
(61.68 KB, 下载次数: 0)
|