zoukankan      html  css  js  c++  java
  • Java_Web--JDBC 增加记录操作模板

    如果不能成功链接数据库,我的博客JAVA中有详细的介绍,可以看一下

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.Statement;  //当如相关驱动包
    
    public class Add {
    
    	public static void main(String[] args) throws Exception {
    
    	    String driver = "com.mysql.cj.jdbc.Driver";  //加载驱动程序,不用改
    	    String userName = "root"; //数据库用户名按自己的改改!!!!!!!!
            String userPwd = "";//数据库密码按自己的改改!!!!!!!!!!!
            String Dbname="students";//以后访问自己数据库的时候按需修改,测试先用这个
            String url = "jdbc:mysql://localhost:3306/"+Dbname+"?&useSSL=false&serverTimezone=UTC";
    	    String coding="&useUnicode=ture&characterEncoding=UTF-8";//编码格式
    		url = url+coding; // 形成带数据库读写编码的数据库连接字
    
    		Class.forName(driver); // 加载并注册驱动程序
    		Connection conn = DriverManager.getConnection(url, userName, userPwd);// 创建连接对象
    		if (!conn.isClosed())
    			System.out.println("Succeeded connecting to the Database!");
    		//Statement stmt = conn.createStatement();// 现在很少有人用了,部分老师比较古板,所以你不写可能会扣分的呀。笑哭
    
    		// 更新(添加、删除、修改)数据库操作
    		String sql = "insert into stu(xh,name,cj) values(2,'李四',98)";
    		PreparedStatement pstmt = conn.prepareStatement(sql);
    		int n = pstmt.executeUpdate(sql);// 返回记录操作条数
    
    		if (n > 0) {
    			System.out.println("添加记录成功,共添加了" + n + "条记录");
    		} else {
    			System.out.println("添加不成功!");
    		}
    		pstmt.close();
    	//	stmt.close();
    		conn.close();
    
    	}
    
    }
    
  • 相关阅读:
    jquery判断复选框是否选中
    jquery验证网址格式
    jquery右下角返回顶部
    thinkphp分页格式的完全自定义,直接输入数字go到输入数字页
    textarea出现多余的空格
    html渐隐轮播
    linux 路由 route
    ansible 自动化运维工具
    数据库 group by 后其他列的取值
    linux 磁盘io高排查
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798295.html
Copyright © 2011-2022 走看看