The Artima Developer Community
Sponsored Link

Java Answers Forum
connection problem

2 replies on 1 page. Most recent reply: Oct 19, 2004 7:40 PM by Yufeng Lu

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Yufeng Lu

Posts: 2
Nickname: beter
Registered: Oct, 2004

connection problem Posted: Oct 18, 2004 8:52 PM
Reply to this message Reply
Advertisement
Hi there:

I have a java program which I want to use it to create table in MS SQL server database. The connection seems successful. But I got NullPointerException. I need help
about it. My program and the error is followed. Thank you so much for your help.

beter



// CreateTables.java

import java.net.URL;
import java.sql.*;
import java.util.*;

/**
* CreateTables is used to create a table used in the project
*
* @author Beter
* @since 10/15/2004
* @version 1.0
*/

public class CreateTables
{
Connection connection;
Statement statement;
PreparedStatement preparedStatement;

public static void main(String[] args) {
try {
CreateTables createTables = new CreateTables();
createTables.create(); /* line 24 */
}
catch (Exception e) {
e.printStackTrace();
}
}

public void create()
throws Exception {

connect();
createAccountTable(); /* line 35 */
closeConnection();
}

public void connect() throws Exception {

try {

// Load JDBC driver
DriverManager.registerDriver(new
com.microsoft.jdbc.sqlserver.SQLServerDriver());

System.out.println("Connecting to Billpayment...");

Connection connection = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://TIGER:1433; DataBaseName=Billpayment;User=sa;Password=beter");
System.out.println("Connection successful");

// Meta data
DatabaseMetaData meta = connection.getMetaData();
System.out.println("\nDriver Information");
System.out.println("Driver Name: "
+ meta.getDriverName());
System.out.println("Driver Version: "
+ meta.getDriverVersion());
System.out.println("\nDatabase Information ");
System.out.println("Database Name: "
+ meta.getDatabaseProductName());
System.out.println("Database Version: "+
meta.getDatabaseProductVersion());


connection.setAutoCommit(false);
} catch(Exception e) {
System.err.println("System Exception in connect");
System.err.println(e);
throw e;
}
}

public void closeConnection() throws Exception {
try {
System.out.println("Closing connection");
connection.close();
}
catch (Exception e)
{
System.err.println("System Exception in closeConnection");
System.err.println(e);
throw e;
}
}

private void createAccountTable()
throws SQLException, Exception {

try {

System.out.println("Creating tables...");
Statement statement = connection.createStatement();/*line 94*/
statement.execute("CREATE TABLE ACCOUNT(" +
"ACCT_ID INT NOT NULL, " +
"NAME CHAR(30) NOT NULL, " +
"USER_PSWD CHAR(20) NOT NULL," +
"EMAIL CHAR(40) NOT NULL, " +
"PHONE_NO CHAR(30) NOT NULL, " +
"BALANCE REAL NOT NULL)");

connection.commit();
statement.close();
}
catch(Exception e) {
System.err.println("System Exception in createAccountTable");
System.err.println(e);
throw e;
}
}
}

C:\Project>javac CreateTables.java

C:\Project>java CreateTables
Connecting to Billpayment...
Connection successful

Driver Information
Driver Name: SQLServer
Driver Version: 2.2.0040

Database Information
Database Name: Microsoft SQL Server
Database Version: Microsoft SQL Server 2000
- 8.00.194 (Intel X86)
Aug 6 2000 00:57:48
Copyright (c) 1988-2000 Microsoft Corporation
Enterprise Evaluation Edition on Windows NT 5.1
(Build 2600: Service Pack 1)

Creating tables...
System Exception in createAccountTable
java.lang.NullPointerException
java.lang.NullPointerExceptio n
at CreateTables.createAccountTable(CreateTables.java:94)
at CreateTables.create(CreateTables.java:35)
at CreateTables.main(CreateTables.java:24)


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: connection problem Posted: Oct 19, 2004 6:53 AM
Reply to this message Reply
Your error lies in the following line.


Connection connection = DriverManager.getConnection( ...

Try tu use
connection = DriverManager.getConnection( ...
instead.


Explanation:

class C {

Connection c;

public C () {
method1();
method2();
}

void method1 {
Connection c = createConnection();
//At this point you should assing c to this.c
}
void method2 {
c.doSomethingVeryImportant();
//this will result in a NullPointerException, because you never assinged c from method1 to this.c
}
}

Yufeng Lu

Posts: 2
Nickname: beter
Registered: Oct, 2004

Re: connection problem Posted: Oct 19, 2004 7:40 PM
Reply to this message Reply
Thank you so much, neumi. It solved my problem. I realy appreciate it.

Beter

Flat View: This topic has 2 replies on 1 page
Topic: output file to notepad Previous Topic   Next Topic Topic: adding ovals

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use