public class Demo{
public static void main(String[]args){
new Demo().addInfo();
new Demo().getInfo();
}
public void getInfo(){
Connection con;
try {
con = getConn();
Statement sta = con.createStatement();
String sql ="select * from t";
ResultSet rs=sta.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(1));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void addInfo(){
Connection con;
try {
con = getConn();
String sql ="insert into t values(?)";
PreparedStatement sta = con.prepareStatement(sql);
String name = new String("我们".getBytes(),"GBK");
sta.setString(1, name);
int count=sta.executeUpdate();
if(count>0){
System.out.println("s");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getConn() throws IOException, ClassNotFoundException, SQLException{
Properties props = new Properties();
InputStream in = Properties.class.getResourceAsStream("/db.properties");
props.load(in);
in.close();
String driver = props.getProperty("driver");
//System.out.println(driver);
String url = props.getProperty("url");
String user = props.getProperty("user");
String pwd = props.getProperty("pwd");
Class.forName(driver);
return DriverManager.getConnection(url);
}
}
如果你希望成功,以恒心为良友,以经验为参谋,以小心为兄弟,以希望为哨兵。