Collection conn = null ;
PreparedStatement st = null ;
ResultSet rs = null ;
try{
Class.forname(driverClassName) ;
conn = DriverManager.getConnection(url,username,password) ;
ps = conn.prepareStatement("select score.* from score,student where score.stuid = student.id and student.name=?") ;
ps.setString(1,studentName) ;
Result rs = ps.executeQuery() ;
while(rs.next()){
system.out.println(rs.getInt("subject")+rs.getFloat("score")) ;
}
catch(Exception e){
e.printStackTrace() ;
}
finally{
if(rs!=null) {rs.close()}
if(ps!=null) {ps.close()}
if(conn!=null) {conn.close}
}
}
或者是
import java.sql.* ;
public class Select{
//定义数据库驱动程序
public static final String driver = "com.mysql.jdbc.Driver" ;
//定义数据库连接
public static final String url = "jdbc:mysql://localhost:3306/user" ;
//定义数据库名称
public static final String username = "root" ;
//定义数据库密码
pulic static final String password = "123" ;
Collection conn = null ;
PreparedStatement ps = null ;
ResultSet rs = null ;
try{
Class.forName(driver) ;
conn = DriverManager.getConnection(url,username,password) ;
ps = conn.getPrepareStatement("select score.* from score,student where score.id = student.id and student.name=null") ;
ResultSet rs = ps.executeQuery() ;
while(rs.next()){
System.out.println(rs.getInt("subject")+rs.getFloat("score"))
}
catch(Exception e){
e.printStackTract() ;
}
finally{
if(rs!=null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace() ;
}
}
if(ps!=null){
try{
ps.close();
}catch(Exception e){
e.printStackTrace() ;
}
}
if(conn!=null){
try{
conn.close() ;
}catch(Exception e){
e.printStackTrace() ;
}
}
}
}
}