java Maven 使用 JDBC 访问 MySql 数据库
在 pom.xml 中加入
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.8</version> </dependency> </dependencies>
我的mysql 版本是 5.1.30
新建 MySqlUtil.java 文件
package com.JavaMySqlJdbc.dbMySql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class MySqlUtil { //是否连接数据库 public static String mysql_open="false"; //数据库连接地址 public static String mysql_url="jdbc:mysql://localhost:3306/callcenter2_db"; //用户名 public static String mysql_username= "root"; //密码 public static String mysql_password ="";//admindljjk //驱动名称 public static String mysql_driver = "com.mysql.jdbc.Driver"; private static Connection conn = null; public static Connection getConn() { try { Class.forName(mysql_driver); conn = DriverManager.getConnection(mysql_url, mysql_username, mysql_password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); closeConn(); } return conn; } public static void closeConn(){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
App.java
import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; import java.util.UUID; import com.JavaMySqlJdbc.dbMySql.MySqlUtil;
public class App { public static void main( String[] args ) { System.out.println( "Hello World!-------JavaMySqlJbdc-------------" ); MySqlUtil.mysql_url = "jdbc:mysql://localhost:3306/callcenter2_db_rencai?useUnicode=true&characterEncoding=utf-8&useSSL=false"; MySqlUtil.mysql_username = "root"; MySqlUtil.mysql_password = ""; MySqlUtil.mysql_driver = "com.mysql.jdbc.Driver"; System.out.println( "url: " +MySqlUtil.mysql_url); //查询---------------------------------------------------------- String strPassword=getPasswordByUserName("管理员8001"); System.out.println( "密码: " +strPassword); //新增-------------------------------------------------------------- //SQL语句 String strId = UUID.randomUUID().toString();//唯一码 String event = "event 2323"; String callid = "callid 132343298432"; String surveyresult = "2"; String sn = "3121231231.02131"; String sql = "insert into ls_satisfaction "; sql = sql + " (id,event1,callid,surveyresult,sn ) "; sql = sql + " values ('"+strId+"','"+event+"','"+callid+"','"+surveyresult+"','"+sn+"' ) "; int intResult=ExecuteUpdate(sql); System.out.println( "执行结果: " +intResult); } private static int ExecuteUpdate(String sql) { Connection conn = MySqlUtil.getConn(); Statement stmt = null; //ResultSet ret = null; int intResult = -1; try { stmt = conn.createStatement(); //执行语句,得到结果集 intResult = stmt.executeUpdate(sql); if(intResult>0) { //影响一行以上 }else{ //没有影响一行 } conn.close();//关闭连接 } catch (SQLException e1) { e1.printStackTrace(); } return intResult; } //查询 private static String getPasswordByUserName(String userName) { //SQL语句 String sql = "select user_password from user where user_name = " + "'" + userName + "'"; Connection conn = MySqlUtil.getConn(); Statement stmt = null; ResultSet ret = null; String password = null; try { stmt = conn.createStatement(); //执行语句,得到结果集 ret = stmt.executeQuery(sql); while (ret.next()) { //这里只查询的密码 password = ret.getString(1); } ret.close(); conn.close();//关闭连接 } catch (SQLException e1) { e1.printStackTrace(); } return password; } } }
注:
使用jdbc连接数据库提示以上错误,上网查了问题大部分都说是jdbc驱动和mysql版本不匹配,目前我mysql版本为5.1.30,
jdbc版本是5.1.40,将jdbc版本改为5.0.8,数据显示正确!!!!
感谢:
小石潭记丶
https://blog.csdn.net/qq_33371766/article/details/80229166
weixin_37250058
https://blog.csdn.net/weixin_37250058/article/details/54019789