这个代码的前提是在MySQL中建了个表,这个表在代码中已经有所说明。
新手写代码,欢迎拍砖!有什么好的建议的尽管说出来。
新手写代码,欢迎拍砖!有什么好的建议的尽管说出来。
import java.sql.*;
public class Test{
public static void main(String[] args){
Connection conn=null;
Statement smt=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/newdb?user=root&password=root");
smt=conn.createStatement();
rs=smt.executeQuery("select*from userinfo");
while(rs.next()){
System.out.print(rs.getInt("id")+" ");
System.out.print(rs.getString("uname")+" ");
System.out.println(rs.getString("upass")+" ");
}
}
catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try{
rs.close();
smt.close();
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
}