Re: Help with java and sql




"debbienc 写道:
"
thax a lot. I will try that.
Debbie
Jeff wrote:
debbienc wrote:
Can someone please help me? I am getting the error message:
[MySQL][ODBC 3.51 Driver][mysqld-5.0.24a-community-nt]No database
selected

When I open up (control panel) (administrative tools) (ODBC) and click
on plantco, then configure and then test, it says success connection
was made, but when I open command prompt and put in C:
cd\
cd j2sdk1.4.2_12\testprog
javac EXO.java
java EXO data2

I get the above error message

This is what I have put in notepad:
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;

public class EXO {

public static void main (String [ ] args ) {

String recordInput=" ";
String sqlCommand= " ";
try {
String url="jdbc:odbc:plantco";
String user="root"; // The user name
String password=""; // The password
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection(url,
user, password);

Statement
sttmnt=conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);

String filename="";
if (args.length > 0)
{
filename = args [0];//Filename
}
else
{
System.out.println("Must supply a file name!");
System.exit(0);
}
BufferedReader inputFile=new BufferedReader
(new FileReader(filename + ".txt"));
sttmnt.executeUpdate("DROP TABLE WinesList");
recordInput=inputFile.readLine();// Read 1st record

sqlCommand="CREATE TABLE WinesList " + recordInput;
//Create SQL statement

sttmnt.executeUpdate(sqlCommand);
//Execute SQL statement

while ((recordInput = inputFile.readLine()) != null)
{
sqlCommand = "INSERT INTO WinesList (Name_wine," +
" on_hand, " + "Color_wine, Cost)";
sqlCommand += "values (" + recordInput + ")";

sttmnt.executeUpdate (sqlCommand); //Execute SQL
System.out.println(sqlCommand);

} // end of while


sttmnt.close();// Close the Statement object
} // end of try

catch ( SQLException s) {// Process SQLException
JOptionPane.showMessageDialog(null, s.getMessage(),
"Database error",
JOptionPane.ERROR_MESSAGE );
System.exit(0);
} // end of catch

catch ( Exception e) {//All other exceptions
JOptionPane.showMessageDialog(null,
e.getMessage( ),
"Driver not found error",
JOptionPane.ERROR_MESSAGE );
System.exit(0);
} //end of catch
} //end of main
} //end of class

Can someone please help me. I have tried everything I can think of,
and looked at several totorials, but I cannot find anything to help me.
Thanks,
Debbie

My understanding of preferences with Java is that ODBC should only be
used for databases for which regular jdbc drivers are not available.
jdbc is cleaner and, well, just works better. So, first suggestion is
to change to J/Connector for your driver rather than the odbc driver.
Then, code such as the following snipped from my current project should
work:
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
// Install MySQL Driver
} catch (Exception e) {
System.err.println("Unable to find and load driver"); //
Error and exit if no MySQL driver
System.exit(1);
}

conn=DriverManager.getConnection("jdbc:mysql://"+ServerNameTxt.getText()+"/mighty",UserNameTxt.getText(),PasswordTxt.getText());

The above snippet occurs on response to the user clicking the OK button
on a login dialog. ServerNameTxt and UserNameTxt and PasswordTxt are
JTextFields on that dialog.

Note the double slash after mysql:. That might be part of your problem
- a malformed URL argument.



I think you must install the software named:mysql-odbc-3.51-Driver and
configure it ,then configure the database source and choose the
database table in the database source,then it will go well ,My English
is poor,Can you understand?

.