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

📄 booklistdb.java

📁 JAVA网上书店的程序原代码,希望对大家偶用
💻 JAVA
字号:
package sample;


import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

public class BookListDB 

{
	  int columnCount,rowCount;
	  
	  ArrayList cellList=new ArrayList();
	  String[] cols=null;
	
	public BookListDB()
	{
		
	}
	
	public void ConnectionDB()
	{
		
		String sqlStr="select * from book";
		Connection con=null;
		Statement st =null;
		ResultSet rs =null;
		PrintWriter pw =null;
		
		
		try {
			Class.forName("org.gjt.mm.mysql.Driver");
			
		} catch (ClassNotFoundException e) {
			
			e.printStackTrace();
			return;
		}
		try {
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" +
										"netbookdata?useUnicode=true&characterEncoding=gb2312","root","abing");
			st = con.createStatement();			
			rs=st.executeQuery(sqlStr);			
			ResultSetMetaData rsmd= rs.getMetaData();   //通过rs.getMetaData() return ResultSetMetaData type object 
			columnCount=rsmd.getColumnCount();
			cols=new String[rsmd.getColumnCount()];  //定义一个列的数组,且通过rsmd.getColumnCount()分配空间
			int len = cols.length;
			for(int i=0; i<len; i++)
			{
				cols[i]=rsmd.getColumnName(i+1);  //save each columnName
				System.out.print("\t"+ cols[i]);
			}			
			System.out.println();
			
			
			while(rs.next())  //get each row
			{
				 ArrayList ll =new ArrayList(); 
				for(int i=0;i<len;i++)         //get each row of colsName
				{
					System.out.print("\t"+getString(rsmd,rs,i+1).trim()); // trim() 返回字符串的副本,忽略前导空白和尾部空白。
				    
				    ll.add(getString(rsmd,rs,i+1).trim());
				}
				System.out.println();				
				cellList.add(ll);
				rowCount++;				
			}
			
		}
		
		catch (SQLException e) {
			
			e.printStackTrace();
			//return;
			
		}
		finally
		{
			if(pw != null)
				try
				{
					pw.close();
				}
				catch(Exception e){e.getMessage();}
				
			if(rs != null)
				try
				{
					rs.close();
				}
				catch(Exception e){e.getMessage();}
				
				if(st!= null)
					try
					{
						st.close();
					}
					catch(Exception e){e.getMessage();}
					if(con != null)
						try
						{
							con.close();
						}
						catch(Exception e){e.getMessage();}
				
		}
		

	}
	
	 public int getColumnCount()  // Column Count
	 {   			
         return  columnCount;
	 }

	 public  int getRowCount()
	 {  						 // Row count
		 return  rowCount;
	 }

	 public String[] getColumnNames()
	 {
		 return this.cols;
	 }

	 public ArrayList getAllData()
	 {
		 return  cellList;
	 }
	
	//判断类型是否匹配的方法
	public  String getString(ResultSetMetaData rsmd, ResultSet rs, int colIndex)
	{
		String result = null;
		int type;
		try {
			type = rsmd.getColumnType(colIndex);
			
			switch(type)
			{
				case Types.CHAR:
				case Types.VARCHAR:
				case Types.LONGVARCHAR:
					result =rs.getString(colIndex);
					break;
				
				case Types.NUMERIC:
				case Types.DECIMAL:
					result =""+ rs.getBigDecimal(colIndex);
					break;
				
				case Types.INTEGER:
					result =""+ rs.getInt(colIndex);
					break;
					
				case Types.FLOAT:
				case Types.DOUBLE:
					result =""+ rs.getDouble(colIndex);
					break;
				
				case Types.BIT:
					result =""+ rs.getBoolean(colIndex);
					break;
					
				case Types.SMALLINT:
					result =""+ rs.getShort(colIndex);
					break;
					
				case Types.TINYINT:
					result =""+ rs.getByte(colIndex);
					break;
					
				case Types.BIGINT:
					result =""+ rs.getLong(colIndex);
					break;
					
				case Types.REAL:
					result =""+ rs.getFloat(colIndex);
					break;
					
				case Types.DATE:
					result =""+ rs.getDate(colIndex);
					break;
					
				case Types.TIME:
					result =""+ rs.getTime(colIndex);
					break;
					
				case Types.TIMESTAMP:
					result =""+ rs.getTimestamp(colIndex);
					break;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	



}

⌨️ 快捷键说明

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