用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

数据库的增删改查

2017-12-22 作者: 柯侧耳倾听者举报

[java]代码库

package mysql;

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

public class TestConnection2 {
	private static  Connection con = null;
	private static ResultSet result=null;
	private static PreparedStatement pre =null;

	public static void main(String[] args) {
		
		
		//1.加载数据库驱动
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("加载失败!");
			e.printStackTrace();
		}
        System.out.println("加载成功!");
	
        //2.连接数据库
        try {
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/db_library", "root", "123");
		} catch (SQLException e) {
			System.out.println("连接数据库失败!");
			e.printStackTrace();
		}
          System.out.println("成功连接数据库!");
          
          //3.处理数据库
          try {
  			 /* pre = con.prepareStatement("create table teacher(id int not null auto_increment,name varchar(20) not null,age int not null,primary key(id))");
  			  int result=pre.executeUpdate();
  			  System.out.println(result);
  		      System.out.println("建表成功!");*/
        	  
        		/*
    			 * insert into table1(name, age) values('李四',22);
    			 */
    			
    			/*String name1="杨旋";
    			int age1=21;
    			pre=con.prepareStatement("insert into teacher(name,age) values(?,?)");
    			pre.setString(1, name1);
    			pre.setInt(2, age1);
    		    pre.executeUpdate();
    		   System.out.println("添加成功!");*/
        	  
        	  
        	  
        	  /*
  			   * update table1 set name='wanger',age=34 where id=1;
  			   */ 
  			/*int id1=1;
  			String name1="何欢";
  			int age1 =22;
  			 pre = con.prepareStatement("update teacher set name=?,age=? where id=?");
  			 pre.setString(1, name1);
  			 pre.setInt(2, age1);
  			 pre.setInt(3, id1);
  		     int result = pre.executeUpdate();
  			 System.out.println(result);
  			  System.out.println("修改成功!");*/
        	  
        	  
        	  /*
        	   * delete from teacher where id=2
        	   */
        	 /* int id1=2;
        	  pre=con.prepareStatement("delete from teacher where id=?");
        	  pre.setInt(1, id1);
        	  pre.executeUpdate();
        	  System.out.println("删除成功!");*/
        	  
        	  /*
        	   * select * from teacher where id>3 
        	   */
        	 
        	  
        	  /*int id1=3;
        	  pre=con.prepareStatement("select * from teacher where id>?");
        	  pre.setInt(1,id1);
        	  result =pre.executeQuery();
        	  while(result.next()){
        		  System.out.println(result.getInt("id"));
        		  System.out.println(result.getString("name"));
        		  System.out.println(result.getInt("age"));
        		  System.out.println("----------------------");
        	  }*/
        	  
        	  /*
        	   * select count(id) from teacher where id>3
        	   */
        	  
        	  int id1=3;
        	  pre=con.prepareStatement("select count(id) from teacher where id>? ");
        	  pre.setInt(1, id1);
        	  result=pre.executeQuery();
        	  if(result.next()){
        		  int count = result.getInt(1);
        		  System.out.println(count);
        	  }
        			    
            } catch (SQLException e) {
          	  System.out.println("执行sql语句出错!");
  			e.printStackTrace();
  			return;
  		}
          
              
          //4.释放数据库的资源
          finally{
        	  try {
        		  if(result!=null)
        		  result.close();
        		  if(pre!=null)
				  pre.close();
        		  if(con!=null)
				  con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
          }
            System.out.println("执行成功!");

	}

}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...