⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linux_access_terminal.java

📁 这是一个linux的终端操作数据库的程序,功能已经和linux自带的差不多,比较完美!
💻 JAVA
字号:
package com.tarena.jdbclab;
import java.sql.*;
import java.io.*;

public class Lab19
{
	public static void main(String[] args){
	  Connection con = null;
	  while((con=getConnection())==null){};
	  try{
	    con.setAutoCommit(false);
	  }catch(Exception e){
	    e.printStackTrace();
	  }
	  process(con);
	  try{
		con.close();
	  }catch(Exception e){
	    e.printStackTrace();
	  }
	  System.out.println("再见!");
	}
	private static Connection getConnection(){
	  Connection con = null;
	  String url = prompt("请输入url:");
	  String userName = prompt("请输入用户名:");
	  String pwd = prompt("请输入密码:");
	  try{
	    con = DriverManager.getConnection(url,userName,pwd);
	  }catch(Exception e){
	    e.printStackTrace();
	  }
	  return con;
	}
	private static void process(Connection con){
	  boolean flag = true;
	  while(flag){
	    String command = getCommand();
	    if("quit".equals(command)){
	      flag = false;
	    }else if("commit".equals(command)
			   || "rollback".equals(command)){
	      processTransaction(con,command);	
	    }else{
	      processSQL(con,command);
	    }
	  }
	}
	private static String getCommand(){
	  String message = "->";
	  StringBuffer sb = new StringBuffer();
	  String command = "";
	  int c = 0;
	  boolean flag = true;
	  while(flag){
		 if(c++!=0){
		   message = c+"->";
		 }
	     sb.append(" "+prompt(message));
		 String tmp = sb.toString().trim();
		 if(tmp.endsWith(";")){
		   flag = false;
		   command = 
			  tmp.substring(0,tmp.length()-1).trim();
		 }
	  }
	  return command;
	}
	private static void processTransaction(Connection con,String command){
	      try{
		    if("rollback".equals(command)){
			   con.rollback();
			}else{
			   con.commit();
			}
		  }catch(Exception e){
		    e.printStackTrace();
			System.out.println("提交或回滚失败!");
		  }
	}
	private static void processSQL(Connection con,String sql){
	   PreparedStatement ps = null;
	   ResultSet rs = null;
	   try{
	     ps = con.prepareStatement(sql);
		 boolean f = ps.execute();
		 if(f){
		   rs = ps.getResultSet();
		   JdbcUtil.printResultSet(rs);
		 }else{
		   int c = ps.getUpdateCount();
		   System.out.println("更新成功-"+c);
		 }
	   }catch(Exception e){
	     e.printStackTrace();
	   }finally{
	     JdbcUtil.close(rs,ps,null);
	   }
	}
	private static String prompt(String message){
	  BufferedReader in = 
		  new BufferedReader(
		     new InputStreamReader(System.in));
	  System.out.print(message);
	  String command = "";
	  try{
	     command = in.readLine();
	  }catch(Exception e ){
	     e.printStackTrace();
	  }
	  return command;

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -