我写了一个JDBC连接数据库代码,想在控制台输入要查询的数据,怎么用Scanner类来实现?
//想把下面那个{Stringsql="selectename,sal,deptnofromemp";}查询语句可以在控制台输入,可以灵活输入任何查询语句,请问怎么来实现...
//想把下面那个{String sql="select ename,sal,deptno from emp";}查询语句可以在控制台输入,可以灵活输入任何查询语句,请问怎么来实现?问题解决另外加分,谢谢大虾们了~~~请根据我下面的代码修改一下~~~
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* jdbc java 操作数据库
* 1,加载驱动 导入jar包
* 2,建立连接
* 3,操作
* 4,释放资源
* @author Administrator
*
*/
public class JdbcDemo {
public static void main(String[] args) {
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try {
//创建连接
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:study", "bjsxt", "bjsxt");
String sql="select ename,sal,deptno from emp";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){
System.out.println("登录成功!");
}else{
System.out.println("登录失败!");
}
while(rs.next()){
Thread.sleep(500);
System.out.println(rs.getString(1)+","+rs.getDouble(2)+","+rs.getInt(3));
}
} catch (Exception e) {
}finally{
try {
if (rs != null) {
rs.close();
}
} catch (Exception e2) {
}
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e2) {
}
try {
if (con != null) {
con.close();
}
} catch (Exception e2) {
}
}
}
} 展开
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* jdbc java 操作数据库
* 1,加载驱动 导入jar包
* 2,建立连接
* 3,操作
* 4,释放资源
* @author Administrator
*
*/
public class JdbcDemo {
public static void main(String[] args) {
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try {
//创建连接
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:study", "bjsxt", "bjsxt");
String sql="select ename,sal,deptno from emp";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){
System.out.println("登录成功!");
}else{
System.out.println("登录失败!");
}
while(rs.next()){
Thread.sleep(500);
System.out.println(rs.getString(1)+","+rs.getDouble(2)+","+rs.getInt(3));
}
} catch (Exception e) {
}finally{
try {
if (rs != null) {
rs.close();
}
} catch (Exception e2) {
}
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e2) {
}
try {
if (con != null) {
con.close();
}
} catch (Exception e2) {
}
}
}
} 展开
展开全部
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* jdbc java 操作数据库
* 1,加载驱动 导入jar包
* 2,建立连接
* 3,操作
* 4,释放资源
* @author Administrator
*
*/
public class JdbcDemo {
public static String getSql() {
Scanner sc = new Scanner(System.in);
return sc.nextLine();
}
public static void main(String[] args) {
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try {
//创建连接
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:study", "bjsxt", "bjsxt");
String sql = getSql()";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){
System.out.println("登录成功!");
}else{
System.out.println("登录失败!");
}
while(rs.next()){
Thread.sleep(500);
System.out.println(rs.getString(1)+","+rs.getDouble(2)+","+rs.getInt(3));
}
} catch (Exception e) {
}finally{
try {
if (rs != null) {
rs.close();
}
} catch (Exception e2) {
}
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e2) {
}
try {
if (con != null) {
con.close();
}
} catch (Exception e2) {
}
}
}
}
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* jdbc java 操作数据库
* 1,加载驱动 导入jar包
* 2,建立连接
* 3,操作
* 4,释放资源
* @author Administrator
*
*/
public class JdbcDemo {
public static String getSql() {
Scanner sc = new Scanner(System.in);
return sc.nextLine();
}
public static void main(String[] args) {
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try {
//创建连接
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:study", "bjsxt", "bjsxt");
String sql = getSql()";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){
System.out.println("登录成功!");
}else{
System.out.println("登录失败!");
}
while(rs.next()){
Thread.sleep(500);
System.out.println(rs.getString(1)+","+rs.getDouble(2)+","+rs.getInt(3));
}
} catch (Exception e) {
}finally{
try {
if (rs != null) {
rs.close();
}
} catch (Exception e2) {
}
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e2) {
}
try {
if (con != null) {
con.close();
}
} catch (Exception e2) {
}
}
}
}
更多追问追答
追问
大哥,我看你了你给的代码,为什么我输入了SQL语句 回车之后就显示登录成功,下面的都没有执行呢?查询的数据都没有出来...数据库连接都是正常的。
追答
你插了多少条数据进去?你如果插一条进去的,if(rs.next())执行了这一句后就到结果集的末端了,while(rs.next())执行不了
展开全部
if(rs.next()){
System.out.println("登录成功!");
}else{
System.out.println("登录失败!");
}
while(rs.next()){//上面已经.rs.next了,所有这里的循环就改成do while吧
Thread.sleep(500);
System.out.println(rs.getString(1)+","+rs.getDouble(2)+","+rs.getInt(3));
}
System.out.println("登录成功!");
}else{
System.out.println("登录失败!");
}
while(rs.next()){//上面已经.rs.next了,所有这里的循环就改成do while吧
Thread.sleep(500);
System.out.println(rs.getString(1)+","+rs.getDouble(2)+","+rs.getInt(3));
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
下面这段代码就是从控制台输入然后再输出的句子,程序运行开始输入,回车则停止,然后将输入内容保存到字符串s中
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
try {
String s = br.readLine();
System.out.println(s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
try {
String s = br.readLine();
System.out.println(s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
又是一个马士兵的学生,你可以通过循环实现
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-08-18
展开全部
sdvcsdvsdvsdvs
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询