1. Write a Singleton out.
The main function of the Singleton pattern is to ensure that in a Java application, only one instance of a class exists.
The general Singleton pattern usually has several forms:
The first form: define a class, its constructor is private, it has a static private variable of the class, when the class is instantiated, get a reference to it through a public getInstance method, and then call it Methods.
public class Singleton {
private Singleton(){}
// Is it weird to define an instance of yourself within yourself ?
//Note that this is private only for internal calls
private static Singleton instance = newSingleton();
//This provides a static method for external access to this class, which can be accessed directly
public static Singleton getInstance() {
return instance;
}
}
Second form:
public class Singleton {
private static Singletoninstance = null;
public staticsynchronized Singleton getInstance() {
//This method is improved than the above, it does not need to generate objects every time, just the first time
//Generate an instance when using it, which improves the efficiency!
if (instance==null)
instance=new Singleton();
return instance;
}
}
Other forms:
Define a class whose constructor is private and all methods are static.
The first form is generally considered to be more secure
2. What are the methods of sorting? Please list. Implement a quick sort in JAVA .
3. There is an array a[n], use java code to reverse the order of the array elements
public class ReverseTest {public int[] reversArray(int[] array) {
int len = array.length;
int temp;
for(int i=0;i<len/2;i++) {
temp = array[i];
array[i] = array[len-i-1];
array[len-i-1] = temp;
}
return array;
} public static void main(String[] args) { //数组初始化必须给定初始的大小 int[] array = {1,2,3,4,5,6,7,8,9,10}; ReverseTest test = new ReverseTest(); int[] reversArray = test.reversArray(array); for(int num: reversArray) { System.out.print(num+" "); } }
}
4. Write a piece of code to connect to the database and operate
package jichu.test;import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class JdbcTest {
private Connection con = null;
private PreparedStatement preparedStatement = null;
public Connection getCon() throws SQLException {
con = (Connection) DriverManager.getConnection("jdbc:mysql:///test","root","tao");
return con;
} public void doSomething() throws SQLException { preparedStatement = (PreparedStatement) getCon().prepareStatement("select*from city where cityName = ?"); preparedStatement.setString(1, "Shanghai");
ResultSet rs = preparedStatement.executeQuery();
while(rs.next()) {
System.out.println(rs.getInt(1)+rs.getString(2));
}
}
public void closeCon() {
if(preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
} } public static void main(String[] args) throws SQLException { JdbcTest jt = new JdbcTest(); jt.doSomething(); jt.closeCon(); }
}