发表于: 2017-07-29 17:26:29

2 860


今天完成:


定名常量:named constant 表示从不改变的永久数据

当需要频繁使用一个常量时,可以用named constant

final datatype CONSTANTNAME = VALUE;

public class Area_1 {

public static void main(String[] args) {
final double PI 3.14159 ;
double radius 20 ;
double area radius*radius*PI;
System.out.println("The area for the circle of radius "+radius+" is "+area);
}
}


查找数组中元素


import java.util.Random;
import java.util.Scanner;

public class findData_1 {
  static int N = 20;

  public static void main(String[] args) {
     int[] arr = new int[N];
     int x;
     int f = -1;

     Random r = new Random();
     for (int i = 0; i < N; i++) {
        arr[i] = r.nextInt(100);
     }
     System.out.println("随机生成的数据序列:");
     for (int i = 0; i < N; i++) {
        System.out.print(arr[i] + " ");

     }
     System.out.print("\n\n");
     for(int j = 0 ; j < 3 ; j++) {
        System.out.print("输入要查找的整数:");
        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);
        x = input.nextInt(); // 输入要查找的数
        for (int i = 0; i < N; i++) {
            f = -1;
           if (x == arr[i]) {
              f = i;
              System.out.println("数据" + x + "位于数组的第" + (f + 1) + "个元素处。");
           }
        }
        if (f < 0) {
           System.out.println("没找到数据:" + x);
        }
     }
     
  }
}


在if循环外,for循环内加上一句:


= -1;



程序正常运行



明天计划:继续学习Spring+JDBC 调试程序 改BUG



遇到的问题:

Spring+jdbc:

项目结构:


部分代码:

package com.jinhege.customer.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.jinhege.customer.D.CustomerDAO;
import com.jinhege.customer.model.Customer;

public class JdbcCustomerDao implements CustomerDAO
{
private DataSource dataSource;

public void setDataSource(DataSource dataSource) {
this.dataSource dataSource;
}

public void insert(Customer customer) {
String sql "INSERT INTO CUSTOMER" "(CUST_ID,NAME,AGE)VALUES(?,?,?)";
Connection conn null;
try {
conn dataSource.getConnection();
PreparedStatement ps conn.prepareStatement(sql);
ps.setInt(1customer.getCustId());
ps.setString(2customer.getName());
ps.setInt(3customer.getAge());
ps.executeUpdate();
//ps.close();
     catch (SQLException e) {
throw new RuntimeException(e);
finally {
if (conn != null) {
try {
conn.close();
catch (SQLException e) {

}
}
}

}

public Customer findByCustomerId(int custId) {
String sql "SELECT *FROM CUSTOMER WHERE CUST_ID = ?";
Connection conn null;
try {
conn dataSource.getConnection();
PreparedStatement ps conn.prepareStatement(sql);
ps.setInt(1custId);
Customer customer null;
ResultSet rs ps.executeQuery();
if (rs.next()) customer new Customer(
rs.getInt("CUST_ID"),
rs.getString("NAME"),
rs.getInt("Age")
);
rs.close();
ps.close();
return customer;
catch (SQLException e) {
throw new RuntimeException(e);
finally {
if (conn != null) {
try {
conn.close();
catch (SQLException e) {

}
}
}
}
}


Console:

改了一下午没有改出来,网上找了很多资料,对比感觉应该没问题才对。明天再继续研究看看吧

七月 29, 2017 5:17:48 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh

信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2e0fa5d3: startup date [Sat Jul 29 17:17:48 CST 2017]; root of context hierarchy

七月 29, 2017 5:17:48 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

信息: Loading XML bean definitions from class path resource [application.xml]

七月 29, 2017 5:17:50 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

信息: Loading XML bean definitions from class path resource [database/Spring-Datasource.xml]

七月 29, 2017 5:17:50 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

信息: Loading XML bean definitions from class path resource [customer/Spring-Customer.xml]

七月 29, 2017 5:17:51 下午 org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName

信息: Loaded JDBC driver: com.mysql.jdbc.Driver

Exception in thread "main" java.lang.RuntimeException: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'name' cannot be null

at com.jinhege.customer.dao.impl.JdbcCustomerDao.insert(JdbcCustomerDao.java:31)

at com.jinhege.common.App.main(App.java:16)

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'name' cannot be null

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)

at com.mysql.jdbc.Util.getInstance(Util.java:408)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)

at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2490)

at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)

at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079)

at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)

at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)

at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)

at com.jinhege.customer.dao.impl.JdbcCustomerDao.insert(JdbcCustomerDao.java:28)

... 1 more

Process finished with exit code 1



注释这句后,程序可以运行,估计插入数据部分有问题,明天再看看

//ps.executeUpdate();    


package com.jinhege.customer.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.jinhege.customer.D.CustomerDAO;
import com.jinhege.customer.model.Customer;

public class JdbcCustomerDao implements CustomerDAO
{
private DataSource dataSource;

public void setDataSource(DataSource dataSource) {
this.dataSource dataSource;
  }

public void insert(Customer customer) {
String sql "INSERT INTO CUSTOMER" "(CUST_ID,NAME,AGE)VALUES(?,?,?)";
Connection conn null;
try {
conn dataSource.getConnection();
PreparedStatement ps conn.prepareStatement(sql);
ps.setInt(1customer.getCustId());
ps.setString(2customer.getName());
ps.setInt(3customer.getAge());
//ps.executeUpdate();    
        ps.close();
     } catch (SQLException e) {
throw new RuntimeException(e);
     } finally {
if (conn != null) {
try {
conn.close();
           } catch (SQLException e) {

           }
        }
     }

  }

public Customer findByCustomerId(int custId) {
String sql "SELECT *FROM CUSTOMER WHERE CUST_ID = ?";
Connection conn null;
try {
conn dataSource.getConnection();
PreparedStatement ps conn.prepareStatement(sql);
ps.setInt(1custId);
Customer customer null;
ResultSet rs ps.executeQuery();
if (rs.next()) customer new Customer(
rs.getInt("CUST_ID"),
rs.getString("NAME"),
rs.getInt("Age")
        );
rs.close();
ps.close();
return customer;
     } catch (SQLException e) {
throw new RuntimeException(e);
     } finally {
if (conn != null) {
try {
conn.close();
           } catch (SQLException e) {

           }
        }
     }
  }
}






收获:

通常,变量用大写字母命名:PI 而非pi或Pi

使用常量的优点:

a,不必重复输入同一个值

b,如果必须修改常量值,只需在源代码中一个地方做改动

c,给常量一个描述性的名字会提高程序易读性



返回列表 返回列表
评论

    分享到