镜像java功能 [英] Mirroring a java functionality
问题描述
嘿伙计我需要你的帮助,
所以下面的代码很简单,它查询数据库表(User)中的所有内容并将这些数据放入结果集中。我只想要表中的第一个名字,但我现在评论它,因为它有效。
public static void main( String [] args) throws 异常
{
// TODO自动生成的方法存根
Class.forName( com.mysql.jdbc.Driver跨度>);
连接con = DriverManager.getConnection( jdbc:mysql://rideon.db。 12096379.hostedresource.com:3306/rideon\", rideon, Rideon12#);
PreparedStatement statement = con.prepareStatement( select * from User) ;
// ResultSet result = statement.executeQuery();
ResultSet result = statement.executeQuery();
closeResultSet(result);
while (result.next())
{
// System.out.println(result.getString(1)++ result.getString(2));
// System.out.println(result.getString(FirstName));
}
}
现在第二个代码检查传递的结果集是否为空。如果不是,则创建第二个语句对象,并将填充结果集的第一个语句对象传递给它并关闭
public static void closeResultSet(ResultSet rs )
{
尝试 {
if (rs!= null )
{
Statement st = rs.getStatement();
System.out.println( 这是声明中的内容: + st);
st.close();
}
} catch (例外e)
{
System.out.println( 蜘蛛猴);
}
}
如果我去打印出第二个声明中的内容,这就是我得到的
打印输出:这是声明中的内容:com.mysql.jdbc.JDBC4PreparedStatement@1a4788f3:select * from User
这是我最初传递给Prepared声明的内容。
所以我的问题是,有没有办法可以镜像C#中的第二个函数(closeResultSet(ResultSet rs))。
感谢您的帮助。
当然你可以在C#中做到这一点 - 有很多选择可以使用MySQL
问题是,你是否只想准确地鹦鹉学习java中的内容? (最不可能) - 或者只是重写C#中的'功能'。
当它全部归结为止时,你有
>
a连接到MySQL
一个或多个查询/数据库操作
获取结果
无这些在MySQL Connector / Net中看起来很难(我最近一直在研究MySQL,因为我需要自己将它用于一个项目)
我看看这个将C#连接到MySQL [ ^ ]开头
MySQL网站有大量C#使用连接器/网络的文档/示例btw
'g'
我曾经提到过有关'直接SQL'与DAL和存储过程的任何内容,这真的取决于你确定方向 - 加上减号[/ edit]
Hey guys I need your help,
So the code below is simple, it query's a database table(User) for everything in it and puts that data in a result set. I wanted only the firstname from the table, but I commented it out for now since it works.
public static void main(String[] args) throws Exception
{
// TODO Auto-generated method stub
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://rideon.db.12096379.hostedresource.com:3306/rideon","rideon","Rideon12#");
PreparedStatement statement = con.prepareStatement("select * from User");
//ResultSet result = statement.executeQuery();
ResultSet result = statement.executeQuery();
closeResultSet(result);
while(result.next())
{
//System.out.println(result.getString(1) + " " + result.getString(2));
//System.out.println(result.getString("FirstName"));
}
}
now this second code checks if the passed result set is not empty. If its not, then a second statement object is created and the first statement object that populated the result set is passed to it and closed
public static void closeResultSet(ResultSet rs)
{
try {
if (rs != null)
{
Statement st = rs.getStatement();
System.out.println("This is what is in the statement:"+ st);
st.close();
}
}catch (Exception e)
{
System.out.println("spider monkey");
}
}
if I go and print out what is in the second statement, this is what I get
Printout: This is what is in the statement:com.mysql.jdbc.JDBC4PreparedStatement@1a4788f3: select * from User
This is what I passed to Prepared statement originally.
So my question is, is there a way I can mirror The second function(closeResultSet(ResultSet rs))in C#.
Thanks for your help.
Of course you can do it in C# - there are plenty of options for working with MySQL
The question is, do you just want to parrot exactly what you have in java ? (least likely) - or just rewrite the 'functionality' in C# .
When it all boils down, you have
a connection to MySQL
One or more Queries/DB Operations
Fetching Results
None of these appear difficult in MySQL Connector/Net (and Ive been studying MySQL recently because I need to use it for a project myself)
I'd have a look at this Connect C# to MySQL[^] to start with
The MySQL site has plenty of documentation/examples for C# using Connector/Net btw
'g'
[edit] I havnt mentioned anything about 'direct SQL' vs a DAL and Stored Procs, that's really up to you to determine direction on - plusses an minusses [/edit]
这篇关于镜像java功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!