发布网友 发布时间:2022-04-22 02:28
共2个回答
懂视网 时间:2022-04-08 01:02
package com.test; import java.sql.*;//引入包 public class MysqlConnTest { public MysqlConnTest() { status = -1; setConnection( null); try { Class. forName( "com.mysql.jdbc.Driver"); //加载驱动 setConnection(DriverManager. getConnection( "jdbc:mysql://localhost:3306/test", "root", "123456")); //建立连接 } catch (Exception e) { e.printStackTrace(); } status = 1; } public static void main(String args[]) throws SQLException{ MysqlConnTest instance = new MysqlConnTest(); if ( instance.getStatus() == 1) { //状态准备好了 instance.TestWrite(); instance.TestRead(); instance.getConnection().close(); } } //测试写程序 public void TestWrite() throws SQLException{ PreparedStatement statement = getConnection().prepareStatement("insert into test(name) values ('hello')"); statement.executeUpdate(); } //测试读程序 public void TestRead() throws SQLException{ PreparedStatement statement = getConnection().prepareStatement("select * from test" ); ResultSet result = statement.executeQuery(); while( result.next()){ System. out.println( "id = " + result.getInt(1) +",name = " + result .getString(2)); } result.close(); } private int status; private Connection connection; public int getStatus(){ return status; } public void setStatus( int status){ this. status = status; } public Connection getConnection() { return connection; } public void setConnection(Connection connection) { this. connection = connection; } }SQL文件:
CREATE DATABASE IF NOT EXISTS `test`; USE `test`; DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(45) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;运行结果:
THE END
Eclipse连接MySQL数据库
标签:java mysql
热心网友 时间:2022-04-07 22:10
1.MySQL安装,创建一个数据
代码如下:
mysql>CREATE DATABASE test; //创建一个数据库
mysql>use test; //指定test为当前要操作的数据库
mysql>CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); //创建一个表user,设置两个字段。
mysql>INSERT INTO user VALUES('huheng','123456'); //插入一条数据到表中
2.打开Eclipse,创建一个项目。
操作:右键点击my--->build Path--->add external Archiver...选择jdbc驱动,点击确定。
3.驱动已经导入,下面我们来写一个程序验证一下
代码如下:
import java.sql.*;
public class MysqlJdbc {
public static void main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序
//Class.forName("org.gjt.mm.mysql.Driver");
System.out.println("Success loading Mysql Driver!");
}
catch (Exception e) {
System.out.print("Error loading Mysql Driver!");
e.printStackTrace();
}
try {
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","198876");
//连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码</p> <p> System.out.println("Success connect Mysql server!");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from user");
//user 为你表的名称
while (rs.next()) {
System.out.println(rs.getString("name"));
}
}
catch (Exception e) {
System.out.print("get data error!");
e.printStackTrace();
}
}
}
点击运行程序:
代码如下:
Success loading Mysql Driver!
Success connect Mysql server!
huheng
出现上面结果,说明你连接数据库成功。
代码如下:
import java.sql.*;
public class MysqlJdbc {
public static void main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序
//Class.forName("org.gjt.mm.mysql.Driver");
System.out.println("Success loading Mysql Driver!");
}
catch (Exception e) {
System.out.print("Error loading Mysql Driver!");
e.printStackTrace();
}
try {
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","198876");
//连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码</p> <p> System.out.println("Success connect Mysql server!");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from user");
//user 为你表的名称
while (rs.next()) {
System.out.println(rs.getString("name"));
}
}
catch (Exception e) {
System.out.print("get data error!");
e.printStackTrace();
}
}
}
点击运行程序:
代码如下:
Success loading Mysql Driver!
Success connect Mysql server!
huheng
出现上面结果,说明你连接数据库成功。
代码如下:
import java.sql.*;
public class MysqlJdbc {
public static void main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序
//Class.forName("org.gjt.mm.mysql.Driver");
System.out.println("Success loading Mysql Driver!");
}
catch (Exception e) {
System.out.print("Error loading Mysql Driver!");
e.printStackTrace();
}
try {
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","198876");
//连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码</p> <p> System.out.println("Success connect Mysql server!");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from user");
//user 为你表的名称
while (rs.next()) {
System.out.println(rs.getString("name"));
}
}
catch (Exception e) {
System.out.print("get data error!");
e.printStackTrace();
}
}
}
点击运行程序:
代码如下:
Success loading Mysql Driver!
Success connect Mysql server!
huheng
出现上面结果,说明你连接数据库成功。
4.可以查看到MySQL里面的内容,那我们是不是想往MySQL中插入数据呢。下面的例子,往MySQL的user表中插入100条数据
代码如下:
import java.sql.*;</p> <p>public class Myjproject {
public static void main(String args[])
{
try {
Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序
//Class.forName("org.gjt.mm.mysql.Driver");
System.out.println("Success loading Mysql Driver!");
}
catch (Exception e) {
System.out.print("Error loading Mysql Driver!");
e.printStackTrace();
}
try {
Connection connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test","root","198876");
int num=100;
PreparedStatement Statement=connect.prepareStatement("INSERT INTO user VALUES(?,?)");
for(int i=0;i<num;i++) //定义个100次的循环,往表里插入一百条信息。
{
Statement.setString(1,"chongshi"+i);
Statement.setString(2,"bo"+i);
Statement.executeUpdate();
}</p> <p> // } catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
// System.out.println("An error has occurred:"+e.toString());
// e.printStackTrace();
}catch(SQLException e)
{
}
}
}
5.下面我们打开MySQL数据库进行查看
代码如下:
mysql> show tatabases; //查看所数据库
mysql> use test; //使test为当前要操作的数据库
mysql> show tables; //查看当前数据库的所有表
mysql> select *from user; //查看当前表(user)的所有信息