这里因为主要的是演示Java程序与数据库的连接,所以表的建立就不详细说了。
Java程序连接mysql数据库
这里的表格是通过控制台创建的一个基本的:
在eclipse的工程环境下:需要导入一个jar包:
/**
* 连接mysql数据库
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class Testttttt{
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/c1604?user=root&password=");
//通过连接创建statement
Statement statament=connection.createStatement();
//定义sql userinfo为要查询相应的表名
String sql="select * from userinfo";
//执行sql语句
ResultSet rs=statament.executeQuery(sql);
List<UserInfoBean> us=new ArrayList<UserInfoBean>();
while(rs.next()){
int id=rs.getInt("id");
String nam