当前位置: 技术问答>java相关
请哪位高手帮我解答!
来源: 互联网 发布时间:2015-10-29
本文导语: import java.sql.*; public class MyPhone { public MyPhone() { String url="jdbc:odbc:phone"; String driver="sun.jdbc.odbc.JdbcOdbcDriver"; Connection con; Statement stmt; ResultSet rs; String query="select * from addresslist"; try { Class.forName("sun.j...
import java.sql.*;
public class MyPhone {
public MyPhone() {
String url="jdbc:odbc:phone";
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
Connection con;
Statement stmt;
ResultSet rs;
String query="select * from addresslist";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
con=DriverManager.getConnection(url,"sa","sa");
stmt=con.createStatement();
stmt.executeUpdate("drop table addresslist");
stmt.executeUpdate("create table addresslist(name varchar(10),telephone bigint,sex varchar(6))");
stmt.executeUpdate("insert into addresslist values('dongxiao','1234567890','male')");
stmt.executeUpdate("insert into addresslist values('alan','2345678901','male')");
rs=stmt.executeQuery(query);
System.out.println("name "+"telephone "+"sex ");
while(rs.next()) {
String a=rs.getString(1);
String b=rs.getString(2);
String c=rs.getString(3);
System.out.println(a+b+c);
}
stmt.close();
con.close();
}
catch (SQLException e) {
System.out.println(e);
}
}
public static void main(String [] args) {
MyPhone phone=new MyPhone();
}
}
这是一个连sqlserver的程序,现在我想从外部输入insert语句的值。例如
stmt.executeUpdate("create table addresslist(name varchar(10),telephone bigint,sex varchar(6))");
中的name、telephone、和sex的值 用Symtem.in.rean()的方式输入,然后查询的时候可以在外部输入查询条件就是比如: where name=XXX, XXX也是由System.in.read()的方式输入,应该怎么办呢?
public class MyPhone {
public MyPhone() {
String url="jdbc:odbc:phone";
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
Connection con;
Statement stmt;
ResultSet rs;
String query="select * from addresslist";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
con=DriverManager.getConnection(url,"sa","sa");
stmt=con.createStatement();
stmt.executeUpdate("drop table addresslist");
stmt.executeUpdate("create table addresslist(name varchar(10),telephone bigint,sex varchar(6))");
stmt.executeUpdate("insert into addresslist values('dongxiao','1234567890','male')");
stmt.executeUpdate("insert into addresslist values('alan','2345678901','male')");
rs=stmt.executeQuery(query);
System.out.println("name "+"telephone "+"sex ");
while(rs.next()) {
String a=rs.getString(1);
String b=rs.getString(2);
String c=rs.getString(3);
System.out.println(a+b+c);
}
stmt.close();
con.close();
}
catch (SQLException e) {
System.out.println(e);
}
}
public static void main(String [] args) {
MyPhone phone=new MyPhone();
}
}
这是一个连sqlserver的程序,现在我想从外部输入insert语句的值。例如
stmt.executeUpdate("create table addresslist(name varchar(10),telephone bigint,sex varchar(6))");
中的name、telephone、和sex的值 用Symtem.in.rean()的方式输入,然后查询的时候可以在外部输入查询条件就是比如: where name=XXX, XXX也是由System.in.read()的方式输入,应该怎么办呢?
|
学生作业吧,?无聊,有点创意好不好,
采用Servlet,或者jsp,用浏览器输入,浏览器显示
至于中的name、telephone、和sex的值 的输入,或者查询条件
可以通过构建sql语句来完成,
例如
sql="insert into addresslist select '"+sex+"'",...
sql="select * from addresslist where name='"+name+"'"
采用Servlet,或者jsp,用浏览器输入,浏览器显示
至于中的name、telephone、和sex的值 的输入,或者查询条件
可以通过构建sql语句来完成,
例如
sql="insert into addresslist select '"+sex+"'",...
sql="select * from addresslist where name='"+name+"'"
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = br.readLine();
也可以用PreparedStatement:
PreparedStatement pstmt = conn.prepareStatement("select * from addresslist where name = ?");
pstmt.setString(1,name);
pstmt.executeQuery();
String name = br.readLine();
也可以用PreparedStatement:
PreparedStatement pstmt = conn.prepareStatement("select * from addresslist where name = ?");
pstmt.setString(1,name);
pstmt.executeQuery();