比如说,我在SQLserver2000中先建立一个表Student.里面有学号S1,S2,S3.包含的列名有Sno,Sn,age..然后在java执行的时候输入S1,这时候能显示出表中对应的数据。package sql;import javax.swing.JOptionPane;
import java.sql.*;
import java.beans.*;public class OneDeath {public static void main(String[] args) {Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:demo","","");
//以上2行代码是连接数据库public Statement (stmt = con.createStatement());
//问题出在这`这是建立一个接口?还是一个构造器.不是很懂。String name = JOptionPane.showInputDialog("想查询什么?请输入学号");
//弹出一个对话框。TextField ntxt=new TextField();
iu=ntxt.getText();
/*这个也不是特懂。
*TextField 对象是允许编辑单行文本的文本组件。
*API中是这么写的。*/ResultSet rs=stmt.executeQuery("select Sno,Sn,Age from Student where Sno = S1");
//这个是把查询出来的那行提取出来System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
//打印提取出来的3列。}}我这么理解对么`但是运行有错误。。
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
语法错误,将“enum Identifier”插入到完整 EnumHeaderName 中
语法错误,将“EnumBody”插入到完整 BlockStatement 中
The member enum $missing$ cannot be local
at sql.OneDeath.main(OneDeath.java:15)
import java.sql.*;
import java.beans.*;public class OneDeath {public static void main(String[] args) {Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:demo","","");
//以上2行代码是连接数据库public Statement (stmt = con.createStatement());
//问题出在这`这是建立一个接口?还是一个构造器.不是很懂。String name = JOptionPane.showInputDialog("想查询什么?请输入学号");
//弹出一个对话框。TextField ntxt=new TextField();
iu=ntxt.getText();
/*这个也不是特懂。
*TextField 对象是允许编辑单行文本的文本组件。
*API中是这么写的。*/ResultSet rs=stmt.executeQuery("select Sno,Sn,Age from Student where Sno = S1");
//这个是把查询出来的那行提取出来System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
//打印提取出来的3列。}}我这么理解对么`但是运行有错误。。
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
语法错误,将“enum Identifier”插入到完整 EnumHeaderName 中
语法错误,将“EnumBody”插入到完整 BlockStatement 中
The member enum $missing$ cannot be local
at sql.OneDeath.main(OneDeath.java:15)
import java.sql.*;
import java.beans.*;
import java.awt.*;public class OneDeath {
public static void main(String[] args) { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:demo","","");
public Statement stmt = con.createStatement();
/*这么写的话`还是报错误。
*类型 Statement 有歧义
*参数 stmt 的修饰符非法;只允许使用终态
这个是什么意思啊`哪错了。。*/
String name = JOptionPane.showInputDialog("想查询什么?请输入学号");
String iu;
TextField ntxt=new TextField();
iu=ntxt.getText();
//这块我把提取出来的值赋给iu.并且把iu定义成String形`这样行么?
ResultSet rs=stmt.executeQuery("select Sno,Sn,Age from Student where Sno = S1");
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
}}
import java.sql.*;
import java.beans.*;
import java.awt.*;public class OneDeath {
public static void main(String[] args) {Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;conn =DriverManager.getConnection("jdbc:odbc:demo","","");
stmt = conn.createStatement();String name = JOptionPane.showInputDialog("想查询什么?请输入学号");String iu;
TextField ntxt=new TextField();
iu=ntxt.getText();
rs=stmt.executeQuery("select Sno,Sn,Age from Student where Sno =" + S1);
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));}}小改了一点,其他的不知道你到底问的是什么意思
Connection conn = null;
Statement stmt = null;//类型 Statement 有歧义
ResultSet rs = null;楼上的这么改`是给几个变量初始化了么?
其他都的没错`但这4行报错了。
TextField ntxt=new TextField();
iu=ntxt.getText(); 有点多余了。你String name = JOptionPane.showInputDialog("想查询什么?请输入学号");
的话就是把你输入的学号赋值给name了啊,那你查询的话就直接引用name就可以了
public Statement stmt = con.createStatement();把public去掉。
ResultSet rs=stmt.executeQuery("select Sno,Sn,Age from Student where Sno = S1");不能改成
rs=stmt.executeQuery("select Sno,Sn,Age from Student where Sno =" + S1);改成这样有两个错误,1是S1是一个已有的学号,你程序本身没声明,二是S1放在这里本身这样写不符合数据库的要求,你的S1是作为什么来用的,你想表达的意思假如是SNO=S1的话要写成"...SNO='"+S1+"'"; 但是你S1已经是一个已经存在的数了,所以此时你的name也就是你想要别人输入的学号就没用的,感觉画蛇添足了。
此时假如是我们平常数据库语言的话想查询学号别入等于123的话应该写成这样
“select Sno,Sn,Age from Student where Sno =‘123’”,注意123有单引号,所以你在想用name作为一个比较变量的话就应该写成这样
“select Sno,Sn,Age from Student where Sno =‘“+name+"'",把两个单引号分开,你假如写成“select Sno,Sn,Age from Student where Sno =“+name就会出错。
我也是刚刚学的,有错的地方见谅。