Question:
java database question help please?
MrJames
2011-03-24 20:46:54 UTC
So I have homework in Java,
Write a Java program that creates a Car table with car manufacturers, models, model years, and fuel efficiency ratings. Insert several cars. Print out the average fuel efficiency. Use CREATE TABLE, INSERT, and SELECT AVG SQL commands.
This has to be done solely in UNIX, like using mysql in unix and everything. I have the JDBC driver and access to mysql in unix. How can I start doing this? How do I connect to the database through java? I really need help getting started. Some code would be greatly appreciated.
Three answers:
deonejuan
2011-03-24 23:04:28 UTC
With NetBeans you could be up in 30 minutes. I don't understand the connectivity you want of UNIX => MySql => Java. Perhaps this link will get you going. One day, after this assignment, take a look at the Java JNA connectivity for doing this. JNA layers the java atop the SQL and you use java.



Good luck.
TheMadProfessor
2011-03-25 06:31:51 UTC
Might want to check the JDK documentation on the various classes and methods in the java.sql package at http://download.oracle.com/javase/6/docs/technotes/guides/jdbc/ but in a nutshell, first you'll need to get a JDBC driver for the specific database software you'll be using. In a given program, a possible sequence of steps:



1) Create and instantiate a Connection object to create a connection with the database

2) Create a String object and set it to your SQL code

3) Create a Statement object and invoke the 'createStatement' method to tie it to your connection object

4) Create a ResultSet object and tie it to the results of the 'executeStatement' method to populate it with rows

5) Have a while loop of invocations of the 'next' method to step thru and process rows of the ResultSet

6) Invoke the 'close' method to close the connection when done



Lots of other possibilities exist, for example creating a PreparedStatement object instead of a regular one (for things like stored procedures or parameterized queries)
?
2016-11-15 00:18:58 UTC
The JDBC™ API replaced into designed to maintain undemanding issues undemanding. this suggests that the JDBC makes regularly occurring database projects undemanding. This path walks you thru examples of utilising JDBC to execute undemanding sq. statements, and carry out different objectives undemanding to database applications. This path is split into those classes: JDBC introduction Lists JDBC features, describes JDBC shape and comments sq. instructions and Relational Database techniques. JDBC fundamentals covers the JDBC API, that's secure interior the Java™ SE 6 launch. by potential of the tip of the 1st lesson, you will comprehend the thank you to apply the common JDBC API to create tables, insert values into them, question the tables, retrieve the end results of the queries, and replace the tables. in this technique, you will the thank you to apply undemanding statements and arranged statements, and additionally you will see an occasion of a stored technique. you will additionally the thank you to accomplish transactions and the thank you to catch exceptions and warnings. « previous • TOC • next »


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...