我正在使用PostgreSQL,我正在尝试运行一个简单的Hibernate应用程序,特别是页面中描述的应用程序.我的hibernate.cfg.xml文件是:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql:testDB2</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">user</property>
<property name="connection.pool_size">1</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2dll.auto">create</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> -->
<mapping class="com.al.hibmaventest.Department"/>
<mapping class="com.al.hibmaventest.Employee"/>
</session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)
我pom.xml(使用maven时)是:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.al.hibmaventest</groupId>
<artifactId>HibMavenTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.6.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.1.GA</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
</dependencies>
<build> …Run Code Online (Sandbox Code Playgroud) 如何在Java中解码 Base64 URL?
我试过发现smt到javadoc但找不到解决方案.
我是否需要为此目的安装任何外部-jars?
代码片段:
public static void main(String []args){
String url = "aHR0cDovL05jLmtwaS51YS9hdXR1bW4yMDEz";
String decodedUrl = Base64.decode(url);
System.out.println( decodedUrl );
return;
}
Run Code Online (Sandbox Code Playgroud)
如何解码这个网址?
我试图解决基于回文的问题.
但我一直得到的是这个例外 - ArrayIndexOutOfBoundsException
我真想自己想出来,但我无法理解原因.
这是代码:
public class LargestPalindrome
{
public int isPalindrome(int n)
{
int revNum=0;
int num=n,flag=0;
while(num!=0)
{
revNum=revNum*10;
revNum=revNum + (num%10);
num=num/10;
}
if(n==revNum)
flag=1;
else
flag=0;
return(flag);
}
public static void main(String args[])
{
LargestPalindrome p=new LargestPalindrome();
int flag=0,pro=0;
int a[]= new int[100];
int b[]=new int[100];
for(int i=1;i<=99;i++)
{
a[i]=i;
b[i]=i;
}
for(int i=1;i<=99;i++)
{
for(int j=1;j<=99;i++)
{
pro=a[i]*b[j];
flag=p.isPalindrome(pro);
if(flag==1)
System.out.println(pro);
pro=0;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?
这是我的一段代码,我想用$符号分割字符串,但字符串不会被吐出.
这是我的代码:
String str="first$third$nine%seventh";
String s[]=str.split("$");
System.out.println(s[0]);
Run Code Online (Sandbox Code Playgroud)
输出是整个字符串:
第一个$第三个$ 9个第七%
我必须使用 GuavaLoadingCache将 xpath 表达式缓存为 xpath 值。
问题在于并非所有 xpath 都有值。大多数时候该值为null.
代码片段:
private LoadingCache<String, List<String>> cachedXpaths = CacheBuilder.newBuilder()
.expireAfterWrite(3, TimeUnit.MINUTES)
.maximumSize(1000)
.concurrencyLevel(5)
.weakKeys()
.build(new CacheLoader<String, List<String>>() {
@Override
public List<String> load(String key) throws Exception {
return createListByKey(key);
}
});
private static List<String> createListByKey(String key) throws Exception {
List<String> values = null;
try {
values = instance.getXpathValues(key);
} catch (XPathExpressionException ignore) {
}
return values;
}
Run Code Online (Sandbox Code Playgroud)
结果:
testEncounterSection(com.epam.cdatest.section.EncountersSectionTest) Time elapsed: 0.002 sec <<< FAILURE!
com.google.common.util.concurrent.UncheckedExecutionException: com.epam.cdatest.exceptions.XpathHasEmptyValueException
at com.epam.cdatest.parsers.XpathEvaluator.getXpathValues(XpathEvaluator.java:123)
at …Run Code Online (Sandbox Code Playgroud) 我想知道为什么Guavas Ordering在以下代码中无法正常工作:
public static void main(String[] args) {
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println(Ordering.natural().isOrdered(integers));
List<String> strings = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
System.out.println(Ordering.natural().isOrdered(strings));
List<String> strings2 = Arrays.asList("6417", "6418", "6419", "6420");
System.out.println(Ordering.natural().isOrdered(strings2));
}
Run Code Online (Sandbox Code Playgroud)
输出:
true
false
true
Run Code Online (Sandbox Code Playgroud)
我预计会看到true3次.
有人可以解释为什么没有按预期排序吗?
我用来HashMap存储斐波那契值.
以下是此代码执行的输出:
Enter n: 500000
F(500000) = 2955561408 ... computed in 5,141 ms
Enter n: 500000
F(500000) = 2955561408 ... computed in 0 ms
Run Code Online (Sandbox Code Playgroud)
它缓存得很好,返回结果很好
我想更好地替换它Guava cache,我已经失去了任何利润.代码执行输出:
Enter n: 500000
F(500000) = 2955561408 ... computed in 5,184 ms
Enter n: 500000
F(500000) = 2955561408 ... computed in 5,086 ms
Run Code Online (Sandbox Code Playgroud)
这是程序代码:
public class CachedFibonacci {
private static Map<BigDecimal, BigDecimal> previousValuesHolder;
static {
previousValuesHolder = new HashMap<>();
previousValuesHolder.put(BigDecimal.ZERO, BigDecimal.ZERO);
previousValuesHolder.put(BigDecimal.ONE, BigDecimal.ONE);
}
private static LoadingCache<BigDecimal, BigDecimal> cachedFibonacci = …Run Code Online (Sandbox Code Playgroud) 我的单例类是这样的:
public class SerializableSingleton implements Serializable {
private static SerializableSingleton instance = new SerializableSingleton();
private SerializableSingleton() {
System.out.println("Constructor is being called");
}
public static SerializableSingleton getInstance() {
return instance;
}
}
Run Code Online (Sandbox Code Playgroud)
现在网上写到,当我们反序列化这个单例对象时,它将返回一个新实例,而不是前一个实例,为了解决这个问题,请使用该readResolve()方法。
但我的问题是——这怎么可能?当静态类成员无法序列化时,如何反序列化它呢?而且全网都这样?
由于单例对象是静态的:
private static SerializableSingleton instance = new SerializableSingleton();
Run Code Online (Sandbox Code Playgroud)
实例首先是如何被序列化的?