到目前为止,只有当我在executionUpdate语句中声明值时,我才能将数据插入到我的SQL表中.我想知道是否有一种方法可以将这些值作为变量传递给我将在执行方法中声明为参数,如下所示:
public void updateSQL(String name, String dnsName, String ipV4, String ipV6, int statusCode)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection("jdbc:sqlserver://servername;database=databasename;integratedSecurity=true");
System.out.println("Database Name: " + connection.getMetaData().getDatabaseProductName());
Statement statement = connection.createStatement();
statement.executeUpdate("INSERT INTO ComputerStatus(Name, DNSName, IPAddressV4, IPAddressV6, StatusCodeID)" + "VALUES(@Name, @DNSName, @IPAddressV4, @IPAddressV6, @StatusCodeID)");
System.out.println("Data Inserted");
ResultSet resultSet = statement.executeQuery("SELECT Name FROM ComputerStatus");
while(resultSet.next())
{
System.out.println("Computer Name: " + resultSet.getString("Name"));
}
connection.close();
}
catch (Exception e)
{
e.printStackTrace();
System.err.println("Problem Connecting!");
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试过几种不同的东西,但到目前为止还没有运气.任何人都知道这是否可以做到?
我写完了一个工作正常的Client/Server Socket通信程序.现在我正在试图弄清楚如何制作它,以便我可以同时与服务器建立多个客户端连接.我环顾四周,似乎有不止两种不同的方法来做到这一点.所以我来这里是为了向你们寻求帮助/建议.
我的服务器:
public class Server {
private ServerSocket serverSocket = null;
private Socket clientSocket = null;
public Server() {
try {
serverSocket = new ServerSocket(7003);
} catch (IOException e) {
System.err.println("Could not listen on port: 7003");
System.exit(1);
}
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed");
System.exit(1);
}
}
public void startServer() throws IOException {
PrintWriter output = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine, outputLine;
outputLine = "Connected to Server"; …Run Code Online (Sandbox Code Playgroud) 我正在编写一个简单的程序,该程序将从MySQL数据库中提取计算机名称,然后将这些名称存储到String数组列表中(这部分工作正常)。之后,我编写了一个类和一个方法,该方法将String作为参数(将是计算机名称),并尝试对其执行ping操作。这是该类的代码:
public class Ping
{
public void pingHost(String hostName)
{
try
{
InetAddress inet = InetAddress.getByName(hostName);
boolean status = inet.isReachable(5000);
if (status)
{
System.out.println(inet.getHostName() + " Host Reached\t" + inet.getHostAddress());
}
else
{
System.out.println(inet.getHostName() + " Host Unreachable");
}
}
catch (UnknownHostException e)
{
System.err.println(e.getMessage() + " Can't Reach Host");
}
catch (IOException e)
{
System.err.println(e.getMessage() + " Error in reaching the Host");
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,UnknownHostException即使我可以手动ping多数计算机,或者将计算机名称硬编码为“ hostName” ,我仍然会被大多数计算机抛出。
这是我的主要样子:
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, …Run Code Online (Sandbox Code Playgroud) 到目前为止,我已经能够在终端中使用javac命令在Linux中编译我的Java程序,但很快我就需要编译一个包含两个或三个类的程序以及main,我不知道这是如何工作的.
我还可以使用javac命令吗?
我有一个包含客户名称列的表.它有大约10-15个不同的客户端,在列中多次出现.有没有办法可以运行一个查询,列出所有不同的客户端并为每个客户端执行计数,以便显示每个客户端在列中出现的次数?我知道在SQL中你可以使用as来分配临时列,但我是LINQ的新手,并且不知道这是否可行.
任何帮助都会很棒,谢谢.
我正在编写一个程序,它基本上通过java发送linux命令,然后打印输出.如果输出只有一行但是多行输出我无法弄清楚我做错了什么,它工作正常.例如,要检查内存使用情况,我使用"free"命令,但它只返回第1行和第3行.这是我的代码:
if (clinetChoice.equals("3"))
{
String command = "free";
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
System.out.println("You Chose Option Three");
String line;
while ((line = reader.readLine()) != null)
{
output += line;
System.out.println(line);
line = reader.readLine();
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时它只返回:
total used free share buffers cached
-/+ buffers/cache: 6546546 65464645
Run Code Online (Sandbox Code Playgroud)
客户代码:
while ((fromServer = input.readLine()) != null)
{
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye"))
break;
System.out.print("Enter your choice: ");
fromClient = stdIn.readLine().trim();
if(fromClient.equals("1"))
{
System.out.println("Client: " + fromClient);
output.println(fromClient); …Run Code Online (Sandbox Code Playgroud) 我已经构建了一个小型Web应用程序,其中主视图显示一个表,管理员可以选择通过使用以下方法从数据库中删除行来删除行:
*.destroy
Run Code Online (Sandbox Code Playgroud)
但是我想保留数据库中的所有条目,但仍希望用户能够从表中删除行并且不确定如何进行此操作.我在考虑使用两个不同的数据库表,但想检查是否有更简单的方法?
这是我的主要观点:
<h1>Student List</h1>
<table class ="Tables">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Date Submit</th>
<th>Floor Preference</th>
<th></th>
</tr>
<% @students.each do |student|%>
<tr>
<td><%= student.f_name %></td>
<td><%= student.l_name %></td>
<td><%= student.created_at %></td>
<td><%= student.floor_pref %></td>
<td><%= button_to 'Remove', student, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Student', new_student_path %>
Run Code Online (Sandbox Code Playgroud) 我正在开发一个基本上通过表单与db表交互的Web应用程序.最近我不得不进行一些更改,这导致我在db表中添加了另一列.我正在使用RubyMine进行开发,无法弄清楚如何更新模型以使其包含新列.我已将其添加到模型中,如下所示:
class Student < ActiveRecord::Base
attr_accessible :f_name, :floor_pref, :l_name, :is_active
validates_presence_of :f_name, :floor_pref, :l_name
end
Run Code Online (Sandbox Code Playgroud)
我还在schema.rb中添加了它:
ActiveRecord::Schema.define(:version => 20130620140537) do
create_table "students", :force => true do |t|
t.string "f_name"
t.string "l_name"
t.string "floor_pref"
t.boolean "is_active"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
Run Code Online (Sandbox Code Playgroud)
并且也在迁移内部如此:
class CreateStudents < ActiveRecord::Migration
def change
create_table :students do |t|
t.string :f_name
t.string :l_name
t.string :floor_pref
t.boolean :is_active
t.timestamps
end
end
end
Run Code Online (Sandbox Code Playgroud)
问题是当我运行rails控制台并查找我的模型对象时,它没有显示我添加的新列(:is_active).它只显示旧列:
>> Student
Loading development environment (Rails 3.2.13)
Switch to …Run Code Online (Sandbox Code Playgroud)