摘要: 1、面向對象的特征有哪些方面
1.抽象:
抽象就是忽略一個主題中與當前目標無關的那些方面,以便更充分地注意與當前目標有關的方面。抽象并不打算了解全部問題,而只是選擇其中的一部分,暫時不用部分細節。抽象包括兩個方面,一是過程抽象,二是數據抽象。
2.繼承:
繼承是一種聯結類的層次模型,并且允許和鼓勵類的重用,它提供了一種明確表述共性的方法。對象的一個新類可以從現有的類中派生,這個過程稱為類...
閱讀全文
JAVA中在運用數組進行排序功能時,一般有四種方法:快速排序法、冒泡法、選擇排序法、插入排序法。
快速排序法主要是運用了Arrays中的一個方法Arrays.sort()實現。
冒泡法是運用遍歷數組進行比較,通過不斷的比較將最小值或者最大值一個一個的遍歷出來。
選擇排序法是將數組的第一個數據作為最大或者最小的值,然后通過比較循環,輸出有序的數組。
插入排序是選擇一個數組中的數據,通過不斷的插入比較最后進行排序。
<1>利用Arrays帶有的排序方法快速排序
1 import java.util.Arrays;
2 public class Test2{
3 public static void main(String[] args){
4 int[] a={5,4,2,4,9,1};
5 Arrays.sort(a); //進行排序
6 for(int i: a){
7 System.out.print(i);
8 }
9 }
10 }
<2>冒泡排序算法
1 public static int[] bubbleSort(int[] args){//冒泡排序算法
2 for(int i=0;i<args.length-1;i++){
3 for(int j=i+1;j<args.length;j++){
4 if (args[i]>args[j]){
5 int temp=args[i];
6 args[i]=args[j];
7 args[j]=temp;
8 }
9 }
10 }
11 return args;
12 }
<3>選擇排序算法
1 public static int[] selectSort(int[] args){//選擇排序算法
2 for (int i=0;i<args.length-1 ;i++ ){
3 int min=i;
4 for (int j=i+1;j<args.length ;j++ ){
5 if (args[min]>args[j]){
6 min=j;
7 }
8 }
9 if (min!=i){
10 int temp=args[i];
11 args[i]=args[min];
12 args[min]=temp;
13 }
14 }
15 return args;
16 }
<4>插入排序算法
1 public static int[] insertSort(int[] args){//插入排序算法
2 for(int i=1;i<args.length;i++){
3 for(int j=i;j>0;j--){
4 if (args[j]<args[j-1]){
5 int temp=args[j-1];
6 args[j-1]=args[j];
7 args[j]=temp;
8 }else break;
9 }
10 }
11 return args;
12 }
以上就是java中的四種排序方法。不同的方法效率不一樣,下面是不同的算法的比較和數據交換時的大O表示。
冒泡排序:比較O(N2) 數據交換O(N2)
選擇排序:比較O(N2) 數據交換O(N)
插入排序:比較O(N2) 復制數據O(N)
在實際應用中,我們要盡量選擇效率高的算法。
1、比較常用
try{
Class.forName("com.mysql.jdbc.Driver");//加載數據庫驅動
String url="jdbc:mysql://localhost:3306/databasename";//數據庫連接子協議
Connection conn=DriverManager.getConnection(url,"username","password");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from tablename");
while(rs.next()){//不斷指向下一條記錄
System.out.println("DeptNo:"+rs.getInt(1));
System.out.println("\tDeptName:"+rs.getString(2));
System.out.println("\tLOC:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(ClassNotFoundException e){
System.out.println("找不到指定的驅動程序類!");
}catch(SQLException e){
e.printStackTrace();
}
2、通過系統的屬性設置
try{
System.setProperty("jdbc.driver","com.mysql.jdbc.Driver");//系統屬性指定數據庫驅動
String url="jdbc:mysql://localhost:3306/databasename";//數據庫連接子協議
Connection conn=DriverManager.getConnection(url,"username","password");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from tablename");
while(rs.next()){//不斷指向下一條記錄
System.out.println("DeptNo:"+rs.getInt(1));
System.out.println("\tDeptName:"+rs.getString(2));
System.out.println("\tLOC:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
3、看起來比較直觀的一種方式,注冊相應的db的jdbc驅動,3在編譯時需要導入對應的lib
try{
new com.mysql.jdbc.Driver();//創建driver對象,加載數據庫驅動
String url="jdbc:mysql://localhost:3306/databasename";//數據庫連接子協議
Connection conn=DriverManager.getConnection(url,"username","password");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from tablename");
while(rs.next()){//不斷指向下一條記錄
System.out.println("DeptNo:"+rs.getInt(1));
System.out.println("\tDeptName:"+rs.getString(2));
System.out.println("\tLOC:"+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
經常忘記怎么連接數據庫,貼出來備用
DBConnection.java
import java.sql.*;
import java.util.Locale;
import java.util.PropertyResourceBundle;
/**
* @author study
*
* 從給定的資源信息中得到數據庫聯接對象
*
*/
public class DBConnection {
Connection conn = null;
/**
* 從給定的資源文件中獲得連接數據庫的參數
*
*/
public boolean getConnect() {
String str_URL = "";
String str_userName = "";
String str_passWord = ""; // the connect passWord
String str_JdbcDriverName = ""; // the connect JDBCDriverName
// Connection con = null;
try {
PropertyResourceBundle configBundle = (PropertyResourceBundle) PropertyResourceBundle
.getBundle("common.jiangbin.dms.product",
new Locale("cn", "CN"));
if (configBundle == null) {
System.out.println("文件product_cn_CN.properties讀入錯誤");
return false;
}
// the connect URL
str_URL = configBundle.getString("ConnectString");
// the connect userName
str_userName = configBundle.getString("UserID");
// the connect passWord
str_passWord = configBundle.getString("Password");
// the connect JDBCDriverName
str_JdbcDriverName = configBundle.getString("JdbcDriverName");
try {
// 加載驅動程序
Class.forName(str_JdbcDriverName).newInstance();
} catch (ClassNotFoundException e) {
System.out.println("Driver not found");
}
// DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
// DriverManager.registerDriver(null);
this.conn = DriverManager.getConnection(str_URL, str_userName,
str_passWord);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* Returns the conn.
*
* @return Connection
*/
public Connection getConn() {
return conn;
}
/**
* Sets the conn.
*
* @param conn
* The conn to set
*/
public void setConn(Connection conn) {
this.conn = conn;
}
}
屬性文件product_cn_CN.properties(用于MSSQL)
ConnectString=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=DocManagerSystem
UserID=sa
Password=sa
JdbcDriverName=com.microsoft.jdbc.sqlserver.SQLServerDriver
屬性文件product_cn_CN.properties(用于MYSQL)
ConnectString=jdbc:mysql://localhost:3306/mydata
UserID=sa
Password=sa
JdbcDriverName=com.mysql.jdbc.Driver
摘要: 1、面向對象的特征有哪些方面
1.抽象:
抽象就是忽略一個主題中與當前目標無關的那些方面,以便更充分地注意與當前目標有關的方面。抽象并不打算了解全部問題,而只是選擇其中的一部分,暫時不用部分細節。抽象包括兩個方面,一是過程抽象,二是數據抽象。
2.繼承:
繼承是一種聯結類的層次模型,并且允許和鼓勵類的重用,它提供了一種明確表述共性的方法。對象的一個新類可以從現有的類中派生,這個過程稱為類...
閱讀全文