qq_38055421
qq_38055421
2018-12-03 05:23

JDBC连接Mysql出现问题!

5
  • java
  • mysql
  • jdbc

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条回答

为你推荐

换一换