您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

eclipse连接mysql_MySQL

2024/3/4 12:59:08发布13次查看
1. 首先安装mysql数据库,具体安装方法可以上百度搜下,也可以按照下面的方法来安装mysql:
安装mysql:
下载xampp,安装;
打开xampp 控制面板,启动mysql
把mysql的执行文件路径添加到path环境变量中来
mysql安装完成
2. 下载mysql的连接jar包:mysql-connector-java-5.1.22-bin.jar
http://download.csdn.net/detail/liujan511536/8972159
3. 启动mysql后,在命令行中进入mysql的root用户:
mysql -u root
然后新建数据库:
create database db;
新建表user:
use db;create table user(id int, name varchar(20));insert into user(id, name) values (1, 'hello');insert into user(id, name) values(2, 'good');
4. 在eclipse中新建java project,然后向该工程中添加mysql-connector-java-5.1.22-bin.jar;5. 接着在刚才的工程中新建类conn,并添加以下代码:
conn.java:
import java.sql.drivermanager;import java.sql.resultset;import java.sql.sqlexception;import java.sql.statement;import com.mysql.jdbc.connection;public class conn { public static void main(string[] args) throws classnotfoundexception, sqlexception{ class.forname(com.mysql.jdbc.driver); connection conn = (connection) drivermanager.getconnection(jdbc:mysql://localhost:3306/db, root, ); statement stmt = conn.createstatement(); resultset rs = stmt.executequery(select * from user); while(rs.next()){ system.out.println(rs.getint(1) + + rs.getstring(2)); } if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (conn != null) conn.close(); }}
运行该工程就可以连接到数据库了。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product