package com.imooc.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
private static final String URL="jdbc:mysql://127.0.0.1:3306/imooc";
private static final String USER="root";
private static final String PASSWORD="toor";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动器
Class.forName("com.mysql.jdbc.Driver");
//2.获得数据库的连接
Connection conn=DriverManager.getConnection(URL, USER, PASSWORD);
//3.操作数据库
conn.createStatement();
Statement stmt= conn.createStatement();
ResultSet rs=stmt.executeQuery("select user_name,age from imooc_goddess");
while(rs.next()){
System.out.println(rs.getString("user_name"+","+rs.getInt("age")));
}
}
}
控制台信息:
Exception in thread "main" java.sql.SQLException: Column 'user_name,22' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1073)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5206)
at com.imooc.db.DBUtil.main(DBUtil.java:23)
yanrun
慕的地6079101
whyareyou
慕粉3853874
because_ofyou