JDBC模板

JDBC 模板样式

相关属性根据个人设置修改

package project;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.io.IOException;
import java.io.InputStream;

public class jdbc {
    
    public static void main(String[] args)  {
        Connection connection = null;
        Statement stmt= null;
        
        try {
        final String URL ="jdbc:mysql://localhost:3306/onlinedb?serverTimezone=UTC&characterEncoding=utf-8" ;
         final  String USERNAME ="root";
         final String PWD="979416";
        //导入驱动加载具体的驱动类
        Class.forName("com.mysql.cj.jdbc.Driver");
            Properties info;
        //与数据库建立连接
         connection = DriverManager.getConnection(URL, USERNAME, PWD);
            //发送sql语句,执行命令
             stmt=connection.createStatement();
            //String sql = "insert into user values(65,123456,'男')";     //增加数据库属性
            //String sql = "update info set ";             //修改数据库属性
             String sql = "delete from info where 1=1";                  //输出数据库属性
             
            //执行sql
            int count = stmt.executeUpdate(sql);
            //处理结果
            if(count>0) {
                System.out.println("操作成功");
            

            }
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch(SQLException e) {
            e.printStackTrace();
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            try {
            
            if(stmt!=null)stmt.close();
            
            if(connection!=null)connection.close();
            }catch(SQLException e) {
                e.printStackTrace();
            }
        }
            
            
        
    }
     
    
    
    
}
原文地址:https://www.cnblogs.com/-xsw000/p/12520447.html