来源:自学PHP网 时间:2015-04-16 10:50 作者: 阅读:次
[导读] 1、首先下载mysql安装包,下载链接mysql下载,然后安装。2、输入安装时的用户名和密码,连接数据库。3、在Eclipse 中安装mysq JDBCl驱动,安装步骤如下图示:测试是否成功连接mql,代码:...
|
1、首先下载mysql安装包,下载链接mysql下载,然后安装。 2、输入安装时的用户名和密码,连接数据库。 3、在Eclipse 中安装mysq JDBCl驱动,安装步骤如下图示:
测试是否成功连接mql,代码:
package day_09;
import java.sql.*;
public class Test
{
public static void main(String[] args)
{
String url="jdbc:mysql://localhost:3306/mydb";
String user="root";
String password="281889";
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
String sql;
sql="insert into student values(2014218,'李月','女')";
stmt.execute(sql);
sql="select * from student";
rs = stmt.executeQuery(sql);
System.out.println("学号" + "\t\t\t" + " 姓名"+"\t\t\t"+"性别");
String name = null;
while(rs.next())
{
name = rs.getString("sname");
System.out.println(rs.getString("sno") + "\t\t\t" + name+"\t\t\t"+rs.getString("sex"));
}
}
catch(ClassNotFoundException e1)
{
System.out.println("数据库驱动不存在!"+e1.toString());
}
catch(SQLException e2)
{
System.out.println("数据库存在异常"+e2.toString());
}
finally
{
try
{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
catch(SQLException e)
{
System.out.println(e.toString());
}
}
}
}
打印结果:
|
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com