小编Jav*_*ast的帖子

使用Java泛型进行JPA findAll()查询和WHERE子句

因此,经过10年多的休整,我将回到Java并尝试使用JPA和Java泛型.我已经创建了一个基于泛型的findAll(other)JPA查询

SELECT * FROM source WHERE other_id = other.id;
Run Code Online (Sandbox Code Playgroud)

这是我要去的地方.它有效,但我想知道是否有更好,更清洁的方法来做到这一点.使用ManagedType很难,并没有太多完整的文档或简单的例子.

我决定尽可能保持我的代码通用(没有双关语),所以我使用JPA2.

这是所有实体类的根.我可能不需要它,但它阻止我有基本的错误.

import java.io.Serializable;

public abstract class DomainObject implements Serializable {

    private static final long serialVersionUID = 1L;

    public abstract void setId(Long id);
    public abstract Long getId();

}
Run Code Online (Sandbox Code Playgroud)

这是抽象的DAO类.我为实现类扩展了这个,因为我需要更具体地做其他活动 - 主要是确保加载延迟集.

public abstract class GenericDAOImpl<T extends DomainObject, T2 extends DomainObject> implements GenericDAO<T, T2> {

private Class<T> type;

@PersistenceContext
protected EntityManager entityManager;

public GenericDAOImpl(Class<T> type) {
    super();
    this.type = type;
}

... save and delete classes go …
Run Code Online (Sandbox Code Playgroud)

java generics jpa criteria-api

16
推荐指数
3
解决办法
8万
查看次数

Java新手需要数据库连接方面的帮助

我是Java的新手,甚至是Java数据库连接的新手.当我把它放在Main类中时,我设法创建了一个数据库连接并查询了一个表.现在我已将它移动到一个名为Connection的新类中,我收到错误:

package lokate;

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

public class Connection {

private static Statement stmt = null;
private static ResultSet rs = null;
private static Connection con = null;

public Connection() throws SQLException {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        String connectionUrl = "jdbc:mysql://localhost:3306/Lokate?" +
                               "user=root&password=";
        con = DriverManager.getConnection(connectionUrl);
        stmt = con.createStatement();
        retriveData("SELECT * FROM Users");
        int rowsEffected = 0;
    } catch (SQLException sqlEx) {
        System.out.println("SQL Exception: "+ sqlEx.toString());
    } catch (ClassNotFoundException classEx) {
        System.out.println("Class Not Found Exception: "+ …
Run Code Online (Sandbox Code Playgroud)

java mysql connection-string database-connection

4
推荐指数
1
解决办法
2072
查看次数

从所需的 .class 文件间接引用,甚至构建路径设置正确 apache POI..?

import java.io.*;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hwpf.*;
import org.apache.poi.hwpf.extractor.*;
import org.apache.poi.hwpf.usermodel.HeaderStories;

public class ReadDocFileInJava {

   public static void main(String[] args) 
   {
      /**This is the document that you want to read using Java.**/
      String fileName = "C:\\Documents and Settings\\kushalp\\Desktop\\Test.doc";

      /**Method call to read the document (demonstrate some useage of POI)**/
      readMyDocument(fileName);
   }
   public static void readMyDocument(String fileName)
   {
         POIFSFileSystem fs = null;
         try 
         {
             fs = new POIFSFileSystem(new FileInputStream(fileName));
             HWPFDocument doc = new HWPFDocument(fs);

             /** Read the content **/
             readParagraphs(doc); …
Run Code Online (Sandbox Code Playgroud)

java eclipse ubuntu apache-poi buildpath

4
推荐指数
1
解决办法
2万
查看次数

从数据库中找到第n行

我是从不同页面捕获的参数.我得到i的值为1它应该显示表的第n行.但它没有显示任何内容.

int i=Integer.parseInt(req.getParameter("index"));
i=i-1;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection  con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","SYSTEM","SYSTEM");
String query="select * from employee1  limit 1 offset i";
PreparedStatement pst=con.prepareStatement(query);
ResultSet rs = pst.executeQuery();

     String name = rs.getString(2); 
     int salary = rs.getInt(3); 
PrintWriter out=res.getWriter();
res.setContentType("text/html");
out.println("<form action='update' method=''>");
    out.println("Name:<input type='text' name='name' value="+name+"/>");
    out.println("Salary:<input type='text' name='salary' value="+salary+ "/>");
    out.println("<input type='submit' name='update' />");
out.println("</form>");
Run Code Online (Sandbox Code Playgroud)

java sql jdbc

2
推荐指数
1
解决办法
390
查看次数

为什么需要Thread.start()?

我工作的主题,当一个问题击中我的mind..If我们可以直接调用run()方法与类的像那么任何普通方法的对象为什么我们需要调用Thread.start()调用的run()方法..我尝试了两种方法,并且两者都得到了相同的结果

首先直接调用run()方法

class Abc extends Thread
{
   public void run()
   {
      for(int i=0;i<5;i++)
      {
         System.out.println("Abc");
      }
      try
      {
          Thread.sleep(100);
      }catch(Exception e)
      {
          System.out.println("Error : "+ e);
      }
   }
}

class Xyz extends Thread
{
    public void run()
    {
       try
       {
           for(int i=0;i<5;i++)
           {
               System.out.println("Xyz");
           }
           Thread.sleep(100);
       }catch(Exception e)
       {
            System.out.println("Error : "+ e);
       }
    }
}

public class ThreadDemo 
{
    public static void main(String[] args) 
    {
        Abc ob=new Abc();
        Xyz oc=new Xyz();
        ob.run();
        oc.run();
    }
}
Run Code Online (Sandbox Code Playgroud)

通过调用Thread.start()进行第二次尝试

public class ThreadDemo …
Run Code Online (Sandbox Code Playgroud)

java multithreading thread-sleep

1
推荐指数
1
解决办法
241
查看次数