java dbc连接musql

Posted weixin_43063239

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java dbc连接musql相关的知识,希望对你有一定的参考价值。

1首先是连接数据库的驱动jar包
网盘可以去提取jar包
链接: https://pan.baidu.com/s/1DBKr1-Sb8rWIDbtg46Bvdw 提取码: x64j
引入jar包:
jar包放入项目src路径下,右击项目porperties>Libraries>add jars选择src路径下添加的jar包并确定。

2连接mysql数据库代码:
查询:

package com.java.csdn; 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Mysql_db 
 
    public static void main(String[] args) 
        //声明Connection对象
        Connection con;
        //驱动程序名
        String driver = "com.mysql.jdbc.Driver";
        //localhost是本地地址,3306mysql的端口号,mybluesky_wl是连接的数据库名
        String url = "jdbc:mysql://localhost:3306/mybluesky_wl";     
        //MySQL配置时的用户名
        String user = "root";
        //MySQL配置时的密码
        String password = "cppcpp277277";
        //遍历查询结果集
            //加载驱动程序
            try 
				Class.forName(driver);
		         //1.getConnection()方法,连接MySQL数据库!!
	            con = DriverManager.getConnection(url,user,password);
	            String sql="SELECT * FROM ordermax";
	            PreparedStatement ps=con.prepareStatement(sql);
	            ResultSet rs=ps.executeQuery();
	            while(rs.next())
	            //stimame 是查询得表的字段,也可以写成数字下标的形式。
	            System.out.println(rs.getString("stuname"));	
	            
			 catch (ClassNotFoundException e) 
				// TODO Auto-generated catch block
				e.printStackTrace();
			 catch (SQLException e) 
				// TODO Auto-generated catch block
				e.printStackTrace();
			
    

3我写了一个增删改查的工具类

package com.java.csdn;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Mysql_db_tool 
		/**
		 * 连接数据库返回connection对象
		 * @return
		 */
		public static Connection getConn()

        Connection con = null;
		String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/mybluesky_wl";
        String user = "root";
        String password = "cppcpp277277";
        
        try 
			Class.forName(driver);
	         //1.getConnection()方法,连接MySQL数据库!!
            con = DriverManager.getConnection(url,user,password);
            System.out.println("连接成功");
		 catch (ClassNotFoundException e) 
			// TODO Auto-generated catch block
			e.printStackTrace();
		 catch (SQLException e) 
			// TODO Auto-generated catch block
			e.printStackTrace();
		
		return con;
	
	
	public static void add(String sql,Connection con)
		Statement stt = null;
		try 
			stt = con.createStatement();
			stt.executeUpdate(sql);
			System.out.println("添加成功");
		 catch (SQLException e) 
			// TODO Auto-generated catch block
			e.printStackTrace();
		finally
            //释放资源
            try  
            	stt.close();
            	con.close();
            	System.out.println("关闭连接");
             catch (Exception e2) 
        
	
	
	public static void query(String sql,Connection con,int tableNo)
		Statement stt = null;
		try 
			stt = con.createStatement();
			ResultSet rs = stt.executeQuery(sql);
			while (rs.next()) 
				for(int i = 1 ; i <= tableNo ; i++)
					System.out.print("\\t"+rs.getString(i));	
				
				System.out.println();
			
			System.out.println("查询成功");
		 catch (SQLException e) 
			// TODO Auto-generated catch block
			e.printStackTrace();
		finally
            //释放资源
            try     
            	stt.close();
            	con.close();
            	System.out.println("关闭连接");
             catch (Exception e2) 
        
	
	
	public static void delete(String sql,Connection con)
		Statement stt = null;
		try 
			stt = con.createStatement();
			stt.executeUpdate(sql);
			System.out.println("删除成功");
		 catch (SQLException e) 
			// TODO Auto-generated catch block
			e.printStackTrace();
		finally
            //释放资源
            try     
            	stt.close();
            	con.close();
            	System.out.println("关闭连接");
             catch (Exception e2) 
        
	
	
	public static void update(String sql,Connection con)
		Statement stt = null;
		try 
			stt = con.createStatement();
			stt.executeUpdate(sql);
			System.out.println("修改成功");
		 catch (SQLException e) 
			// TODO Auto-generated catch block
			e.printStackTrace();
		finally
            //释放资源
            try     
            	stt.close();
            	con.close();
            	System.out.println("关闭连接");
             catch (Exception e2) 
        
	

miain入口:
package com.java.csdn;

import java.sql.Connection;
import java.sql.SQLException;

public class Mysql_db_main 

	public static void main(String[] args) 
	 	Connection con = Mysql_db_tool.getConn();
	 	//add
//	 	String sql = "INSERT INTO ORDERMAX VALUES(3,'LONGZHE','2019-07-22 11:08:36','2019-07-22 11:08:46','C',1)";
//	 	Mysql_db_tool.add(sql, con);
	 	//query
	 	String sql = "select * from ordermax";
	 	Mysql_db_tool.query(sql, con, 5);
	 	//delete
//	 	String sql = "delete from ordermax where spnrid = 3";
//	 	Mysql_db_tool.delete(sql, con);
	 	//update
//	 	String sql = "update ordermax set spnrid = 3 ,username = 'wangli' where spnrid = 1";
//	 	Mysql_db_tool.update(sql, con);
	 	
	 	
	

这个工具类每次增删改查的方法执行后都会关闭连接,所有不支持循环添加。

以上是关于java dbc连接musql的主要内容,如果未能解决你的问题,请参考以下文章

Java_dbc连接数据库_使用读取配置文件的方式

如何从反应式 MariaDB 连接创建 JOOQ DSLContext

R2DBC 无法使用 h2 协议创建连接

Java中log.append怎么用,啥意思

用Spring Reactive(R2DBC)连接MSSQL。

Spring data r2dbc:与 mysql 连接时出现问题 - 找不到“r2dbcEntityTemplate”