
May 23rd, 2004, 01:30 AM
|
 |
Contributing User
|
|
Join Date: May 2004
Posts: 118
Time spent in forums: 17 h 28 m
Reputation Power: 5
|
|
To connect a java program to a MySQL database, you should download the driver. This driver can be downloaded at http://www.mysql.com. I suppose you're working with Tomcat ? In that case, you should put the downloaded file (jar file for Java ARchive) in Tomcat 4.1\common\lib.
To get connection to the database, you should write
Class.forName("com.mysql.jdbc.Driver").newInstance();
dbconn = DriverManager.getConnection("jdbc:mysql://localhost/gco");
To look things up, write :
StringBuffer searchString = new StringBuffer();
searchString.append("select * from mlzro");
PreparedStatement ps = dbconn.prepareStatement(searchString.toString());
ResultSet rs = ps.executeQuery();
With the resultSet rs you can read the records, and place its columns in an object
But frankly, I'd like to give you an advice. Don't use JSP's alone. Use JSP's in combination with servlets and follow the MVC (model view controller). It seems like hard work but it pays off when you want a maintainable and scalable application.
|