mohamadiiiii
یک شنبه 04 تیر 1391, 11:08 صبح
سلام دوستان من به تازگی برنامه نویسی با جاوا رو شروع کردم و یه برنامه باید بنویسم که اطلاعات رو بگیره و توی دیتا بیس درج کنه اما من هرکاری میکنم به دیتا بیس وصل نمیشه کد اتصال رو هم براتون میذارم در ضمن من از sqljdbc_4.0 استفاده میکنم. ممنون میشم منو از این سردرگمی نجات بدین در آخر هم باید بگم که من تو محیط eclipse کار میکنم ،پیشاپیش از لطف دوستان ممنونم.
import java.sql.*;
public class JDBCSelect {
private static Connection link;
private static Statement statement;
private static ResultSet results;
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
"databaseName=student;user=;password=;";
link = DriverManager.getConnection(connectionUrl);
}
catch(ClassNotFoundException cnfEx) {
System.out.println("Class Error: " + cnfEx.toString());
System.exit(1);
}
catch(SQLException sqlEx) {
System.out.println("Driver Erroe: " + sqlEx.toString());
System.exit(1);
}
try {
statement = link.createStatement();
String select = "SELECT * FROM personal";
results = statement.executeQuery(select);
}
catch(SQLException sqlEx) {
System.out.println("* Cannot execute query! *");
sqlEx.printStackTrace();
System.exit(1);
}
try {
System.out.println();
while (results.next()) {
System.out.println("Name: " + results.getInt("Name"));
System.out.println("\n");
}
}
catch(SQLException sqlEx){
System.out.println("* Error retrieving data! *");
sqlEx.printStackTrace();
System.exit(1);
}
try {
link.close();
}
catch(SQLException sqlEx) {
System.out.println("* Unable to disconnect! *");
sqlEx.printStackTrace();
System.exit(1);
}
}
}
خطا که نشون میده :
Driver Erroe: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'ras'. ClientConnectionId:6f1da5f4-cc4c-4f80-8742-3663c1dbde88
import java.sql.*;
public class JDBCSelect {
private static Connection link;
private static Statement statement;
private static ResultSet results;
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
"databaseName=student;user=;password=;";
link = DriverManager.getConnection(connectionUrl);
}
catch(ClassNotFoundException cnfEx) {
System.out.println("Class Error: " + cnfEx.toString());
System.exit(1);
}
catch(SQLException sqlEx) {
System.out.println("Driver Erroe: " + sqlEx.toString());
System.exit(1);
}
try {
statement = link.createStatement();
String select = "SELECT * FROM personal";
results = statement.executeQuery(select);
}
catch(SQLException sqlEx) {
System.out.println("* Cannot execute query! *");
sqlEx.printStackTrace();
System.exit(1);
}
try {
System.out.println();
while (results.next()) {
System.out.println("Name: " + results.getInt("Name"));
System.out.println("\n");
}
}
catch(SQLException sqlEx){
System.out.println("* Error retrieving data! *");
sqlEx.printStackTrace();
System.exit(1);
}
try {
link.close();
}
catch(SQLException sqlEx) {
System.out.println("* Unable to disconnect! *");
sqlEx.printStackTrace();
System.exit(1);
}
}
}
خطا که نشون میده :
Driver Erroe: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'ras'. ClientConnectionId:6f1da5f4-cc4c-4f80-8742-3663c1dbde88