qq_38055421 2018-12-03 05:23 采纳率: 0%
浏览 769
已采纳

JDBC连接Mysql出现问题!

Insert 类 调用 JdbcUtil类时出现异常,但代码没有红叉,请大神指教!

//第一个类

package com.jdbc.util;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Properties;

public class JdbcUtil {

    //数据库的连接的URL
    private static String url = null;
    //数据库用户名
    private static String user = null;
    //数据库密码
    private static String password = null;
    //驱动程序类
    private static String driverClass = null ;

public static void main(String[] args) {

//static {

    //注册驱动     
    try {       
            //创建Properties对象,接入jdbc.properties配置文件
              Properties prop = new Properties();
            //使用类路径方式读取配置文件
              InputStream in = JdbcUtil.class.getResourceAsStream("/jdbc.properties");
            //加载文件
              prop.load(in);
            //读取配置文件的内容
               url = prop.getProperty("url");
               user = prop.getProperty("user");
               password = prop.getProperty("password");
               driverClass = prop.getProperty("driverClass");
               Class.forName(driverClass);
                System.out.println(url);
                System.out.println(user);
                System.out.println(password);
                System.out.println(driverClass);

                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
               } catch (Exception e) {
                    e.printStackTrace();
              }       
  }

//获取连接方法

public static Connection getConn() {

try {
    Connection conn = DriverManager.getConnection(url, user, password);
    return conn;
} catch (SQLException e) {
    e.printStackTrace();
     throw new RuntimeException(e);
}

}

//释放资源方法

public static void close(ResultSet rs,Statement stmt,Connection conn) {

if(rs != null) {     
   try {
        rs.close();
       } catch (SQLException e) {
         e.printStackTrace();
         throw new RuntimeException(e);
      }
    } 

if(stmt != null) {
    try {
        stmt.close();
        } catch (SQLException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
      }
   }

if(conn != null) {
    try {
          conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
          throw new RuntimeException(e);
      }
  }

}

}

//jdbc.properties配置文件:

url=jdbc:mysql://localhost:3306/test

user=root

password=root

driverClass=com.mysql.jdbc.Driver

//第二个类
package com.crud.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.jdbc.util.JdbcUtil;

public class Insert {

public static void main(String[] args) {
     test();

}
public static void test() {
Connection conn = null;
PreparedStatement stmt =null;
ResultSet rs = null ;

try {
    //获取连接
    conn = JdbcUtil.getConn();
    String sql = "INSERT INTO student(id,name,age) VALUES(?,?,?)";

    //创建prepareStatement对象
    stmt = conn.prepareStatement(sql);
    //设置参数
    stmt.setInt(1, 7);
    stmt.setString(2, "奥巴马");
    stmt.setInt(3, 22);
    //发送参数到数据库
    rs = stmt.executeQuery();
} catch (Exception e) {
    e.printStackTrace();
}finally {
    //关闭资源
     JdbcUtil.close(rs, stmt, conn);
}

}

}

图片说明

  • 写回答

4条回答 默认 最新

  • 爱码少年 00fly.online 2018-12-03 05:56
    关注

    删 public static void main(String[] args) {
    放开 static

    over!

    更新(insert、update、delete)用 executeUpdate

    算了,给你代码吧,自己理解

     package com.jdbc.util;
    
    //import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class JdbcUtil
    {
        // 数据库的连接的URL
        private static String url = null;
    
        // 数据库用户名
        private static String user = null;
    
        // 数据库密码
        private static String password = null;
    
        // 驱动程序类
        private static String driverClass = null;
    
    
        public static void main(String[] args)
        {
            System.out.println(getConn());
        }
    
        /**
         * 只注册一次,静态代码块
         */
        static
        {
            // 注册驱动
            try
            {
                // 创建Properties对象,接入jdbc.properties配置文件
                Properties prop = new Properties();
                // 使用类路径方式读取配置文件
                InputStream in = JdbcUtil.class.getResourceAsStream("/jdbc.properties");
                // 加载文件
                prop.load(in);
                // 读取配置文件的内容
                url = prop.getProperty("url");
                user = prop.getProperty("user");
                password = prop.getProperty("password");
                driverClass = prop.getProperty("driverClass");
                Class.forName(driverClass);
                System.out.println(url);
                System.out.println(user);
                System.out.println(password);
                System.out.println(driverClass);
    
            }
            catch (ClassNotFoundException e)
            {
                e.printStackTrace();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    
        /**
         * 获取连接方法
         */
    
        public static Connection getConn()
        {
            try
            {
                Connection conn = DriverManager.getConnection(url, user, password);
                return conn;
            }
            catch (SQLException e)
            {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    
        /**
         * 释放资源方法
         */
        public static void close(ResultSet rs, Statement stmt, Connection conn)
        {
            if (rs != null)
            {
                try
                {
                    rs.close();
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
    
            if (stmt != null)
            {
                try
                {
                    stmt.close();
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
    
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }
    
    }
    
    

    package com.crud.util;

    import java.sql.Connection;
    import java.sql.PreparedStatement;

    import com.jdbc.util.JdbcUtil;

    public class Insert
    {

    public static void main(String[] args)
    {
        test();
    }
    
    public static void test()
    {
        Connection conn = null;
        PreparedStatement stmt = null;
    
        try
        {
            // 获取连接
            conn = JdbcUtil.getConn();
            String sql = "INSERT INTO student(id,name,age) VALUES(?,?,?)";
    
            // 创建prepareStatement对象
            stmt = conn.prepareStatement(sql);
            // 设置参数
            stmt.setInt(1, 7);
            stmt.setString(2, "奥巴马");
            stmt.setInt(3, 22);
            // 发送参数到数据库
            stmt.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            // 关闭资源
            JdbcUtil.close(null, stmt, conn);
        }
    }
    

    }

    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多