2012-03-01 6 views
1

こんにちは、私は、次のファイルをhtmlファイルにmysqlデータベースに接続する必要があります。しかし、私はそれを接続することに問題があります。誰も私が場所を見つける場所を教えてもらえますか? 私のコンピュータで "jdbc:mysql:// localhost/zulfiqar"を置き換えるにはどうすればいいですか?どこでこれを見つけるのですか? それは私のコンピュータ上で動作させるために変更する必要がある何か他にありますか?これはインターネット上で見つけたコードであり、私は仕事をしようとしているので、それをやる方法を理解することができますが、私は苦労しています。 ありがとうございます!mysqlとhtmlのjdbc接続

import java.io.*; 
import java.sql.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 

public class ServletUserEnquiryForm extends HttpServlet{ 
public void init(ServletConfig config) throws ServletException{ 
super.init(config); 
} 
/**Process the HTTP Get request*/ 
public void doPost(HttpServletRequest req, 
HttpServletResponse res) throws ServletException, 
IOException{ 
String connectionURL = "C:\Program Files(x86)\MySQL Server 5.0\bin\mysql.exe"; 
Connection connection=null; 
ResultSet rs; 
res.setContentType("text/html"); 
PrintWriter out = res.getWriter(); 
//get the variables entered in the form 
String uId = req.getParameter("userId"); 
String fname = req.getParameter("firstname"); 
String sname = req.getParameter("surname"); 
String address1 = req.getParameter("address1"); 
String address2 = req.getParameter("address2"); 
String town = req.getParameter("town"); 
String county = req.getParameter("country"); 
String zipcode = req.getParameter("zipcode"); 
try { 
// Load the database driver 
Class.forName("org.gjt.mm.mysql.Driver"); 
// Get a Connection to the database 
connection = DriverManager.getConnection 
(connectionURL, "root", "admin"); 
//Add the data into the database 
String sql = 
"insert into emp_details values (?,?,?,?,?,?,?,?)"; 
PreparedStatement pst = 
connection.prepareStatement(sql); 
pst.setString(1, uId); 
pst.setString(2, fname); 
pst.setString(3, sname); 
pst.setString(4, address1); 
pst.setString(5, address2); 
pst.setString(6, town); 
pst.setString(7, county); 
pst.setString(8, zipcode); 
int numRowsChanged = pst.executeUpdate(); 
// show that the new account has been created 
out.println(" Hello : "); 
out.println(" '"+fname+"'"); 
pst.close(); 
} 
catch(ClassNotFoundException e){ 
out.println("Couldn't load database driver: " 
+ e.getMessage()); 
} 
catch(SQLException e){ 
out.println("SQLException caught: " 
+ e.getMessage()); 
} 
catch (Exception e){ 
out.println(e); 
} 
finally { 
// Always close the database connection. 
try { 
if (connection != null) connection.close(); 
} 
catch (SQLException ignored){ 
out.println(ignored); 
} 
} 

答えて

0

どのように失敗するかについての追加情報が役立つ場合があります。

1)ソケット接続に失敗した(サービスが実行されていないことを意味します)か、 2)ドライバを初期化できませんでしたか?私はあなたがリストしたものに慣れていません。もっと一般的な方法は "sun.jdbc.odbc.JdbcOdbcDriver"です。 3)接続して、単にユーザー "root"とパスワード "admin"による認証に失敗しましたか?

+0

接続しているかどうかわかりません。これは本当に新しいもので、全体をどのように設定するのか混乱しています。 – user1162494

+0

org.gjt.mm.mysql.Driverとcom.mysql.jdbc.Driverは同じです。ここを参照http://stackoverflow.com/a/5808184/778687 – tusar

0

i "はJDBC // localhost /をズルフィカール:mysqlの" 何を交換する必要があります。それは、接続URLで

アンスで。これはあなたのMySQLデータベースがローカルホストサーバー(デフォルトポートを使用)上で実行されており、あなたが 'zulfiqar'データベースに接続していることを意味します。だから、()のdoPost下の最初の行は次のようになります。

String connectionURL = "jdbc:mysql://localhost/zulfiqar"; 

次に、JDBC接続のためのorg.gjt.mm.mysql.Driverドライバを使用しています。それは当初、趣味主義者によって開発されました。後でMySQLに寄付され、パッケージ/クラス名の名前が変更されました。古いクラス名は下位互換性のために残されていますが、com.mysql.jdbc.Driverに更新し、WEB-INF/libフォルダにmysql-connector-java - * - bin.jarを追加する必要があります。

使用している次の事:

connection = DriverManager.getConnection(connectionURL, "root", "admin"); 

をですから、connectionURLからの接続をロードし、rootユーザーとadminパスワードでアクセスしています。あなたのケースでこれらが正しいことを確認してください。

最後の点は、の表に挿入しています。必要なすべての列を含むzulfiqarデータベースにこのテーブルが作成されていることを確認してください。そして、 '?' sql文字列内のマークは、実行する回数がpst.setString(index, data)と一致する必要があります。それ以外の場合は、無効なパラメータインデックスがエラーになります。