JDBC_Statement
插入数据
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, NoSuchMethodException, InvocationTargetException, IOException {
InputStream is = Jdbc_test.class.getClassLoader().getResourceAsStream("jdbc.propertise");
Properties properties = new Properties();
properties.load(is);
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driver = properties.getProperty("driver");
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,user,password);
Statement statement = conn.createStatement();
Scanner sc = new Scanner(System.in);
System.out.println("请输入账户");
String username = sc.next();
System.out.println("请输入密码");
String userpassword = sc.next();
String sql = "insert into emp values(default,'"+username+"','"+userpassword+"')";
statement.execute(sql);
statement.close();
conn.close();
}
查询数据
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, NoSuchMethodException, InvocationTargetException, IOException {
//加载配置文件
InputStream is = Jdbc_test.class.getClassLoader().getResourceAsStream("jdbc.propertise");
Properties properties = new Properties();
properties.load(is);
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driver = properties.getProperty("driver");
//连接数据库
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,user,password);
//查询结果
Statement statement = conn.createStatement();
String sql = "select * from emp;"; //SQL查询语句
ResultSet rs = statement.executeQuery(sql); //执行查询语句
//打印结果
while(rs.next()){
System.out.println(rs.getString("user")+" "+
rs.getString("password"));
}
//关闭对象
rs.close();
statement.close();
conn.close();
}