因此,经过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的新手,甚至是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) 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) 我是从不同页面捕获的参数.我得到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) 我工作的主题,当一个问题击中我的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 ×5
apache-poi ×1
buildpath ×1
criteria-api ×1
eclipse ×1
generics ×1
jdbc ×1
jpa ×1
mysql ×1
sql ×1
thread-sleep ×1
ubuntu ×1