import java.sql.*;
import java.util.ResourceBundle;
import java.util.Scanner;
public class JDBCTest08 {
public static void main(String[] args) {
ResourceBundle bundle=ResourceBundle.getBundle("jdbc");
Scanner s=new Scanner(System.in);
System.out.println("输入desc或者asc,desc升序,asc降序");
String keywords=s.nextLine();
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
//注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取链接
conn= DriverManager.getConnection(bundle.getString("url"),bundle.getString("user"),bundle.getString("password"));
//获取预编译的数据库对象
stmt=conn.createStatement();
//执行sql
String sql="select ename from emp order by ename "+keywords;
rs=stmt.executeQuery(sql);
while (rs.next()){
System.out.println(rs.getString("ename"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
if(rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(stmt!=null)
{
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn!=null)
{
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
必须使用statment情况:用户需要注入mysql,例如查找排序
最新推荐文章于 2024-07-15 19:44:21 发布