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

📄 test_frist.java

📁 java与数据库相连接的原程序
💻 JAVA
字号:
package main;
//创建配置文件
import java.sql.*;
import java.util.*;
import java.lang.*;
import java.io.*;

public class test_frist {

	/**
	 * @param args
	 */
	static String url;
	static String driver;
	static String user;
	static String password;
	
	public static void ini(String inputname) throws IOException
	{
		//String line = "";
		InputStream is = new FileInputStream(inputname);
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		url = (reader.readLine()).substring(4);
		driver = (reader.readLine()).substring(7);
		user = (reader.readLine()).substring(5);
		password = (reader.readLine()).substring(9);
		is.close();
	}
	
	public static void rewrite (String outfilename)throws IOException{
		String line = "";
		BufferedReader buf;
    	buf= new BufferedReader(new InputStreamReader(System.in));
		OutputStream os = new FileOutputStream(outfilename);
		PrintWriter writer = new PrintWriter(new OutputStreamWriter(os));
		System.out.print("Do you want to rewrite the file ,y 是,n 否:");
    	line = (buf.readLine()).trim();
    	if(line =="n"){
    		//System.out.println(e);
    	}
    	else
    	{
    		System.out.print("Please input the url:");
    		line = buf.readLine();
    		writer.println("url="+line);
    		System.out.print("Please input the driver:");
    		line = buf.readLine();
    		writer.println("driver="+line);
    		System.out.print("Please input the user:");
    		line = buf.readLine();
    		writer.println("user="+line);
    		System.out.print("Please input the password:");
    		line = buf.readLine();
    		writer.println("password="+line);
    		writer.flush();
    	}
    	os.close();
	}
	
	public static void main(String[] args) throws IOException,SQLException{
		 
		//file test = new file();
       // String url = "jdbc:oracle:thin:@:xiaoran";
        ini("c:\\TT.txt");
        String sql = "select empno,ename from emp";
        try{
        	Class.forName(driver);
        	Connection con = DriverManager.getConnection(url,user,password);
        	Statement stat = con.createStatement();
        	ResultSet rs = stat.executeQuery(sql);
        	System.out.println("empno"+"   "+"ename");
        	while(rs.next()){
        			System.out.println((rs.getString(1)).trim()+"   "+(rs.getString(2)).trim());
        		}
//        	int j=1;
//        	while(rs.next()){
//        		while(){
//        			System.out.print(rs.getString(j).trim()+"   ");
//        			j++;
//        		}
//        		j=1;
//        		System.out.println();
//        	}
        	stat.close();
        }
        catch(Exception e)
        {
        	System.out.println(e);
        	String line = "";
    		BufferedReader buf;
        	buf= new BufferedReader(new InputStreamReader(System.in));
        	System.out.print("Please input the path:");
        	line = buf.readLine();
    		OutputStream os = new FileOutputStream(line);
    		PrintWriter writer = new PrintWriter(new OutputStreamWriter(os));
//    		System.out.print("Do you want to rewrite the file ,y 是,n 否:");
        	//line = (buf.readLine()).trim();
//    		line = buf.readLine().trim();
//        	if(line =="no"){
//        		System.out.println(e);
 //       	}
//        	else
 //       	{
        		System.out.print("Please input the url:");
        		line = buf.readLine();
        		writer.println("url="+line);
        		System.out.print("Please input the driver:");
        		line = buf.readLine();
        		writer.println("driver="+line);
        		System.out.print("Please input the user:");
        		line = buf.readLine();
        		writer.println("user="+line);
        		System.out.print("Please input the password:");
        		line = buf.readLine();
        		writer.println("password="+line);
        		writer.flush();
//        	}
        	os.close();
        }
        finally
        {
        	//rewrite();
        	//System.out.println("Successful!");
        	//System.out.println("Wonderful!");
        }
	}

}

⌨️ 快捷键说明

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