/**
* Created by root on 16-3-29.
*/
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Driver;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class JdbcTest {
public static void main(String[] args) throws Exception{
JdbcTest jdbcTest=new JdbcTest();
jdbcTest.connect();
}
//jdbc:mysql://localhost:3306 3306是本地mysql的端口号
public String jdbc_learningURL="jdbc:mysql://localhost:3306/jdbc_database"; //连接本地mysql数据库 jdbc_database
public void connect() throws Exception{
Class.forName("com.mysql.jdbc.Driver"); //注册驱动
java.sql.Connection connection=DriverManager.getConnection(jdbc_learningURL,"root","root"); //建立连接 Connection的类型是java.sql
java.sql.Statement statement=connection.createStatement(); //建立执行sql语句的对象
String query1="SELECT * FROM jdbc_learning"; //SQL语句
ResultSet resultSet=statement.executeQuery(query1); //执行SQL语句 得到ResultSet对象
int columnCount=resultSet.getMetaData().getColumnCount();
while (resultSet.next()){
System.out.println(resultSet.getString("name")); //根据字段名返回对象并转换为java的String类型
}
//关闭连接
resultSet.close();
statement.close();
connection.close();
}
}