java连接mysql数据库代码(java连接数据库的代码)
大家好,今天来为大家分享java连接mysql数据库代码的一些知识点,和java连接数据库的代码的问题解析,大家要是都明白,那么可以忽略,如果不太清楚的话可以看看本篇文章,相信很大概率可以解决您的问题,接下来我们就一起来看看吧!
java是怎么连接mysql数据库的
使用java连接MySQL数据库与其他的数据库连接核心是一样的,如果说区别,那就是所需的驱动不一样。
工具/原料
MySQL、JDK
方法/步骤
1、首先需要安装好JDK(配置环境变量),如图所示:
2、其次要安装好MySQL数据库,可以使用可视化Navicar For MySQL,如图所示:
3、最后通过代码进行连接。
(1)确定连接路径URL:
String url="jdbc:mysql://localhost(可以是本机IP地址):3306(端口号)/mysqltest(数据库名称)?"+"user=用户账号&password=用户密码&useUnicode=字符编码";
(2)加载驱动:
Class.forName("com.mysql.jdbc.Driver");
(3)连接,获取Connection对象
Connection conn=DriverManager.getConnection(url)
(4)可以通过conn对象检验连接与否。
怎样连接mysql数据库java代码
首先你要导包
JDBC连接数据库
•创建一个以JDBC连接数据库的程序,包含7个步骤:
1、加载JDBC驱动程序:
在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机),
这通过java.lang.Class类的静态方法forName(String className)实现。
例如:
try{
//加载MySql的驱动类
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
System.out.println("找不到驱动程序类,加载驱动失败!");
e.printStackTrace();
}
成功加载后,会将Driver类的实例注册到DriverManager类中。
2、提供JDBC连接的URL
•连接URL定义了连接数据库时的协议、子协议、数据源标识。
•书写形式:协议:子协议:数据源标识
协议:在JDBC中总是以jdbc开始子协议:是桥连接的驱动程序或是数据库管理系统名称。
数据源标识:标记找到数据库来源的地址与连接端口。
例如:
(MySql的连接URL)
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk;
useUnicode=true:
表示使用Unicode字符集。如果characterEncoding设置为 gb2312或GBK,本参数必须设置为true。characterEncoding=gbk:字符编码方式。
3、创建数据库的连接
•要连接数据库,需要向java.sql.DriverManager请求并获得Connection对象,该对象就代表一个数据库的连接。
•使用DriverManager的getConnectin(String url, String username, String password)方法传入指定的欲连接的数据库的路径、数据库的用户名和密码来获得。
例如://连接MySql数据库,用户名和密码都是root
String url="jdbc:mysql://localhost:3306/test";
String username="root";
String password="root";
try{
Connection con= DriverManager.getConnection(url, username, password);
}catch(SQLException se){
System.out.println("数据库连接失败!");
se.printStackTrace();
}
4、创建一个Statement
•要执行SQL语句,必须获得java.sql.Statement实例,Statement实例分为以下3种类型:
1、执行静态SQL语句。通常通过Statement实例实现。
2、执行动态SQL语句。通常通过PreparedStatement实例实现。
3、执行数据库存储过程。通常通过CallableStatement实例实现。
具体的实现方式:
Statement stmt= con.createStatement(); PreparedStatement pstmt= con.prepareStatement(sql); CallableStatement cstmt= con.prepareCall("{CALL demoSp(?,?)}");
5、执行SQL语句
Statement接口提供了三种执行SQL语句的方法:executeQuery、executeUpdate和execute
1、ResultSet executeQuery(String sqlString):执行查询数据库的SQL语句,返回一个结果集(ResultSet)对象。
2、int executeUpdate(String sqlString):用于执行INSERT、UPDATE或 DELETE语句以及SQL DDL语句,如:CREATE TABLE和DROP TABLE等
3、execute(sqlString):用于执行返回多个结果集、多个更新计数或二者组合的语句。具体实现的代码:
ResultSet rs= stmt.executeQuery("SELECT* FROM..."); int rows= stmt.executeUpdate("INSERT INTO..."); boolean flag= stmt.execute(String sql);
6、处理结果两种情况:
1、执行更新返回的是本次操作影响到的记录数。
2、执行查询返回的结果是一个ResultSet对象。
• ResultSet包含符合SQL语句中条件的所有行,并且它通过一套get方法提供了对这些行中数据的访问。
•使用结果集(ResultSet)对象的访问方法获取数据:
while(rs.next()){
String name= rs.getString("name");
String pass= rs.getString(1);//此方法比较高效
}
(列是从左到右编号的,并且从列1开始)
7、关闭JDBC对象
操作完成以后要把所有使用的JDBC对象全都关闭,以释放JDBC资源,关闭顺序和声明顺序相反:
1、关闭记录集
2、关闭声明
3、关闭连接对象
if(rs!= null){//关闭记录集
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(stmt!= null){//关闭声明
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(conn!= null){//关闭连接对象
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
java连接数据库的代码
用这个类吧.好的话,给我加加分.
import java.sql.*;
/**
*@功能:一个JDBC的本地化API连接类,封装了数据操作方法,只用传一个SQL语句即可
*@作者:李开欢
*@日期: 2007/
*/
public class ConnectionDemo{
/*
*这里可以将常量全部放入另一个类中,以方便修改
*/
private static Connection conn;
private static Statement ps;
private static ResultSet rs;
private static final String DRIVER="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
private static final String USER="sa";
private static final String PASS="sa";
public ConnectionDemo(){
// TODO Auto-generated constructor stub
ConnectionDemo.getConnection();
}
public static Connection getConnection(){
System.out.println("连接中...");
try{
Class.forName(ConnectionDemo.DRIVER);
conn= DriverManager.getConnection(ConnectionDemo.URL, ConnectionDemo.USER, ConnectionDemo.PASS);
System.out.println("成功连接");
} catch(ClassNotFoundException e){
// TODO Auto-generated catch block
e.printStackTrace();
} catch(SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static Statement getStatement(String sql){
System.out.println("执行SQL语句中...");
try{
ps= conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
if(sql.substring(0, 6).equals("select")){
rs= ps.executeQuery(sql);
System.out.println("执行完查询操作,结果已返回ResultSet集合");
}else if(sql.substring(0, 6).equals("delete")){
ps.executeUpdate(sql);
System.out.println("已执行完毕删除操作");
}else if(sql.substring(0, 6).equals("insert")){
ps.executeUpdate(sql);
System.out.println("已执行完毕增加操作");
}else{
ps.executeUpdate(sql);
System.out.println("已执行完毕更新操作");
}
} catch(SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
return ps;
}
public static ResultSet getResultSet(){
System.out.println("查询结果为:");
return rs;
}
public static void closeConnection(){
System.out.println("关闭连接中...");
try{
if(rs!= null){
rs.close();
System.out.println("已关闭ResultSet");
}
if(ps!= null){
ps.close();
System.out.println("已关闭Statement");
}
if(conn!= null){
conn.close();
System.out.println("已关闭Connection");
}
} catch(Exception e){
// TODO: handle exception
}
}
public static void main(String[] args){
// TODO Auto-generated method stub
ConnectionDemo.getConnection();
String sql="delete from type where id= 1";
ConnectionDemo.getStatement(sql);
String sql2="insert into type values(1,'教学设备')";
ConnectionDemo.getStatement(sql2);
String sql1="select* from type";
ConnectionDemo.getStatement(sql1);
ResultSet rs= ConnectionDemo.getResultSet();
System.out.println("编号"+"类型");
try{
while(rs.next()){
System.out.print(""+rs.getInt(1)+"");
System.out.println(rs.getString(2));
}
} catch(SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
ConnectionDemo.closeConnection();
}
}
OK,关于java连接mysql数据库代码和java连接数据库的代码的内容到此结束了,希望对大家有所帮助。