java数据库连接

 联系我们     |      2019-12-09 04:03

jdbc连接mysql

 

Test.java

 

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;

public class Test {
 static Connection con; // 声明Connection对象
 static Statement sql; // 声明Statement对象
 static ResultSet res;

 public Connection getConnection() { // 连接数据库方法
  try {
     Class.forName( "org.gjt.mm.mysql.Driver" );
   con = DriverManager.getConnection(
     "jdbc:mysql://127.0.0.1:3306/test", "root",
     "0000");

  } catch (Exception e) {
   e.printStackTrace();
  }
  return con; // 返回Connection对象
 }

 // ///////////////

 public static void insertdata() {
  Test c = new Test();
  con = c.getConnection();

  try {
   sql = con.createStatement();

   sql.executeUpdate("  insert  into  stu  values(12,'Shara' ) ;");


  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

}

Main.java



public class Main {

 public static void main(String[] args) {
  // TODO Auto-generated method stub

  Test.insertdata();
  System.out.println("finish");

 }

}

 

输出 : finish

图片 1

回想要加载jar别忘了

图片 2

 

Test.java import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Connection;import java.sql.Statement;public class Tes...

    因为每次使用 JDBC 都要写一些重复的代码,我们可以直接抽取出一个工具类来使用。我们把主要目的是想通过工具类返回数据库连接,然后就可以直接通过连接来操作了。

JDBCUtilsConfig工具类

package utils;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtilsConfig {

private static Connection conn;
private static String driverClass;
private static String url;
private static String user;
private static String password;

static {

    try {
        //1.先调用加载配置文件的方法,读取配置文件
        ReadingConfig();

        //2.加载驱动
        Class.forName(driverClass);

        //3.连接数据库
        conn = DriverManager.getConnection(url, user, password);


    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


//读取配置文件
private static void ReadingConfig() throws Exception {

    //1.使用类加载器读取配置文件
    InputStream inStream = JDBCUtilsConfig.class.getClassLoader().getResourceAsStream("database.properties");

    //2.获取Properties对象
    Properties pro = new Properties();

    //3.加载输入流
    pro.load(inStream);

    //4.解析对应的资源
    driverClass = pro.getProperty("driver");
    url = pro.getProperty("url");
    user = pro.getProperty("user");
    password = pro.getProperty("password");

}


//返回数据库连接
public static Connection Getconnection() {
    return conn;
}


//关闭连接,释放资源
public static void CloseSource(ResultSet rs,PreparedStatement ptmt,Connection conn) {
    if(rs != null) {
        try {
            rs.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    if(ptmt != null) {
        try {
            ptmt.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();
        }
    }
}

}