• JAVA------8.连接mysql数据库


     1 package com.caipei.thread;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.IOException;
     5 import java.io.InputStreamReader;
     6 import java.io.OutputStreamWriter;
     7 import java.io.Writer;
     8 import java.net.ServerSocket;
     9 import java.net.Socket;
    10 import java.sql.Connection;
    11 import java.sql.DriverManager;
    12 import java.sql.ResultSet;
    13 import java.sql.SQLException;
    14 import java.sql.Statement;
    15 import java.text.SimpleDateFormat;
    16 import java.util.Calendar;
    17 import java.util.Random;
    18 
    19 public class ServerDemo {
    20     //crc 校验
    21     private static CrcUtil demo;
    22     //str 转字节
    23     private static StringToByteutil strToByte;
    24     private static  Connection conn = null;
    25     private SimpleDateFormat formatter = new SimpleDateFormat(
    26             "yyyy-MM-dd HH:mm:ss");
    27     static String driver = "com.mysql.jdbc.Driver";
    28     // URL指向要访问的数据库名******
    29     //static String url = "jdbc:mysql://:3306/hhxficdz_schema";
    30     static String url = "jdbc:mysql://localhost:3306/hhxficdz_schema";
    31     // MySQL配置时的用户名
    32     static String user = "";
    33     // Java连接MySQL配置时的密码******
    34     //static String password = "";
    35     static String password = "123456";
    36     //crc校验
    37     private static CrcUtil crc;
    38     private static  Connection getconn() {
    39         //驱动程序名//不固定,根据驱动
    40         try {
    41             Class.forName(driver);
    42             conn = DriverManager.getConnection(url, user, password);
    43         } catch (SQLException e) {
    44             // TODO Auto-generated catch block
    45             e.printStackTrace();
    46         } catch (ClassNotFoundException e) {
    47             // TODO Auto-generated catch block
    48             e.printStackTrace();
    49         }
    50         // System.out.println(props.getProperty("mysql.connection.driver_class"));
    51         return conn;
    52     }

    处理数据库

     1 ResultSet rs = null;
     2                 Statement stmt = null;
     3                 String state=null;
     4                 String eqid=null;
     5                 String returnNun=null;
     6                 conn = getconn();
     7                 stmt = conn.createStatement();
     8 
     9                 // 01:查询充电桩状态
    10                 if (taskNum.equals("01")) 
    11                 {
    12                     try {
    13                         // 要执行的SQL语句eqnum中的项。
    14                         String sql = " select * from t_eqregister where eqnum='"+ choNum + "'";
    15                         rs = stmt.executeQuery(sql);  
    16                         //输出id值和content值
    17                         while(rs.next()) {
    18                             flag=0;
    19                             //充电桩状态
    20                             state = rs.getString("state");
    21                             //如果充电桩状态是 3 将充电桩状态 置0
    22                             if (Integer.valueOf(state)==3) {
    23                                 //修改充电桩注册表的状态为1 
    24                                 String eqsql = " update t_eqregister t set t.state=0 where t.eqnum='"+choNum+"'";
    25                                 stmt.execute(eqsql);
    26                             }
    27 
    28                             //校验码之前字符串
    29                             String leftstr="FF"+"00"+sb.substring(4,12)+"0"+state;
    30                             int le=leftstr.length()/2;
    31                             //校验码计算
    32                             String yanNum=String.valueOf(Integer.toHexString(demo.CRC16(strToByte.stringToByte(leftstr),le))).toUpperCase();
    33                             returnNun=leftstr+yanNum;
    34 
    35                         } 
    36                         rs.close(); 
    37                         conn.close();  
    38                     }  catch (SQLException e) {
    39                         // TODO Auto-generated catch block
    40                         e.printStackTrace();
    41                     } 
    42                 }
  • 相关阅读:
    iOS打包Framework真机和模拟器兼容合并版本
    iOS同一项目多个Target的快速实现方法
    Android横屏下Fragment界面重叠问题
    项目实战工具类(一):PhoneUtil(手机信息相关)
    浅谈FloatingActionButton(悬浮按钮)
    LeanCloud数据存储相关问题
    Android项目实战(二十三):仿QQ设置App全局字体大小
    Android项目实战(二十二):启动另一个APP or 重启本APP
    Android项目实战(二十):浅谈ListView悬浮头部展现效果
    浅谈DrawerLayout(抽屉效果)
  • 原文地址:https://www.cnblogs.com/coriander/p/6546757.html
Copyright © 2020-2023  润新知